PHP Help on preg_replace

iosoft

PC enthusiast since MS DOS 5
Skilled
Hi

I am facing problem to use the PHP function preg_replace.

Actually, I have to replace :
<h1> to <h1><hr>\n
<h2> to <h2><hr>\n
<h3> to <h3><hr>\n
<h4> to <h4><hr>\n
<h5> to <h5><hr>\n

....Don't ask why :bleh:

I can do it using str_replace function 5 times.
But I think it can be done in a single preg_replace()

Pls help me :huh:
 
Try

Code:
$str = '<h1><h2><h3><h4><h5>'; // Whatever string you want to convert

$str = preg_replace('/<h(\d)>/',"<h$1><hr>\n",$str);

Should work. Haven't done comprehensive tests. Only tested with above string.

~

Thomas
 
I need to replace NULL for this pattern -

Code:
* *</p>

NOTE: * is not actually star. I mean to say * = SPACE or ' ' or '\n' of any combination :S
 
By null, I suppose you want to replace then with the text "" or no text.

Code:
$str = "

\n  \n </p> foobar";

$str = preg_replace('/

( |(\&nbsp\;)|\n)*<\/p>/s',"",$str);

//or I think this would also work

$str = str_replace(array("\n"," "," "),'',$str); // But then the 

</p> will remain in the text

~

Thomas
 
One more similar help.

A URL can be any of these formats -

Notice the xid.

I want that, whatever value is passed to the xid, its value (99 here) will be replaced with 64 (constant) always.

:huh: :huh: :huh:
 
^ Working like a charm :clap:

Can't give you repu at this moment due to system restriction.
But my memory is GOOD so u will get it soon ;)
 
Hi

another help on that 'pattern matching system' in PHP.

I have a long HTML page.
from it I have to fetch a particular section -

START with:
Code:
<TABLE ### ID="scoretable" ###>
### means other html-tag-parameters.
END before:
<DIV ID="midsection">

$RAW holds the entire HTML content.
Pls help me out :)
 
Back
Top