Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

preg replace parsing

Posted on 2009-03-31
4
Medium Priority
?
260 Views
Last Modified: 2012-05-06
In the code snippet is an input string, how would I change that first string into the below string using preg_replace in php.

Thanks,

- tim
Hello <span class="my-headline">Tim</span> how are you today.
 
 
Hello <b>Tim</b>

Open in new window

0
Comment
Question by:tdillon80
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Author Comment

by:tdillon80
ID: 24032451
I want to stipulate that there may be other html tags in the string so I don't want all tags to become bold, only the one specified.
0
 
LVL 13

Expert Comment

by:marchent
ID: 24033588
output:
Hello <b>Tim</b>
$str = 'Hello <span class="my-headline">Tim</span> how are you today.';
$str = preg_replace('|<span class="my-headline">(.*?)</span>.*|', '<b>$1</b>', $str);
print "$str\n";

Open in new window

0
 
LVL 13

Accepted Solution

by:
marchent earned 400 total points
ID: 24033595
output:
Hello <b>Tim</b> how are you today.
$str = 'Hello <span class="my-headline">Tim</span> how are you today.';
$str = preg_replace('|<span class="my-headline">(.*?)</span>|', '<b>$1</b>', $str);
print "$str\n";

Open in new window

0
 
LVL 14

Expert Comment

by:shobinsun
ID: 24036726
Hello,

Use this:

<?php
$data = "Hello <span class='my-headline'>Tim</span> how are you today.";
echo $data.'<br>';
$find ="/ how are you today/";
$replace =" ";
$find1 ="/Tim/";
$replace1 ="<b>Tim</b>";
$data1 = preg_replace ($find, $replace, $data);
Echo preg_replace ($find1, $replace1, $data1);
?>

Regards.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question