Solved

preg replace parsing

Posted on 2009-03-31
4
253 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
  • 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 100 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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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…
The viewer will learn how to count occurrences of each item in an array.

705 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