Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Preg_replace expression Problem again!

Status
Not open for further replies.

JimFL

Programmer
Jun 17, 2005
131
GB
Hi,

I am having a few problems with a Preg_replace expression to replace a Ul tag with one that contains a class.

<?php

$ul = "<ul>";
$ulrep = "<ul class=\"verdana12_black\" >";
$str = preg_replace($ul,$ulrep,$str);

?>

It prints out the following
<
>
Test Ul>
on my Html page.

Can anyone help?
 
I certainly wouldn't use preg_replace() in this application, as you're not looking for something that must be described by a regular expression, but rather a string.

This script:
Code:
<?php 

$str = '<ul><li>foo</li><li>bar</li></ul>';
$ul = '<ul>';
$ulrep = '<ul class="verdana12_black">';
$str = str_replace($ul,$ulrep,$str);

print $str;

?>

outputs:

<ul class="verdana12_black"><li>foo</li><li>bar</li></ul>


<aside>Notice my use of singlequotes to delineate strings that contain doublequotes. I've found that getting rid of those internal backslashes improves readability</aside>


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks that seems to have done the trick. I guess I was using the wrong method.
 
It's very important to tailor your searching needs to what it is you're searching for. The regular-expression engine, though powerful, takes up a lot of resources in a script.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top