How can I replace to sub strings (html code)?

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
Betty_S
Forum Newbie
Posts: 20
Joined: Tue Dec 05, 2006 3:40 am

How can I replace to sub strings (html code)?

Post by Betty_S »

How can I replace to sub strings (html code)?
Hi,
I have that:

Code: Select all

$x = 	". . .
	<div class="somecalss"> . . .
	. . .
	. . .
	</div>
	. . .";
$y = "some html code";.
How can I replace the <div> part in $x with $y?
I now that I can do it with RegEx (regular expression) and str_replace but how to define a RegEx?
How can do that?
Is it something like that:

Code: Select all

$r='/\'<div class="somecalss">+.+'</div>''/\';
And then what?

Thanks.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

check out preg_replace() and friends
User avatar
jyhm
Forum Contributor
Posts: 228
Joined: Tue Dec 19, 2006 10:08 pm
Location: Connecticut, USA
Contact:

Post by jyhm »

Or you could get basic and make it down right literal. Best option no but:

Code: Select all

<?php
$string = '<div> html code </div>';
$pattern =array("'<div>'", "'</div>'");
$replacement =array("'[div]'", "'[/div]");
echo preg_replace($pattern, $replacement, $string);
?>
Post Reply