Page 1 of 1

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

Posted: Mon Feb 12, 2007 10:27 pm
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.

Posted: Mon Feb 12, 2007 10:43 pm
by Kieran Huggins
check out preg_replace() and friends

Posted: Tue Feb 13, 2007 4:57 pm
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);
?>