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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
count download link and run update query 9 54
what is best version of php to use 6 44
<? versus <?php 5 35
Why my select dropdown does not work? 8 20
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

947 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now