preg replace parsing

Posted on 2009-03-31
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.


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

Open in new window

Question by:tdillon80
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

Author Comment

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.
LVL 13

Expert Comment

ID: 24033588
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

LVL 13

Accepted Solution

marchent earned 100 total points
ID: 24033595
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

LVL 14

Expert Comment

ID: 24036726

Use this:

$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);


Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

690 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