Page 1 of 1

Removing instances of text from strings

Posted: Mon Aug 11, 2003 9:38 am
by RTT
Hi, here's my problem to solve.

Imagine I have a string, $bodytext, which consists of ->

Code: Select all

<p>Hello world
<br>hello world
<br><img src="someimage.jpg">
<br>hello world
<br><img src="anotherimage.jpg">
<br>hello world
<br><img src="andanotherimage.jpg">
What I need to do, is strip (remove) every instance of "<img src="path">" from the origonal and load those instances into an array.

So I want to be left with the string consisting of ->

Code: Select all

<p>Hello world
<br>hello world
<br>
<br>hello world
<br>
<br>hello world
<br>
and then the new array consisting of ->

Code: Select all

array(int) &#123;
  &#1111;"1"]=>
  string(int) "<img src="someimage.jpg">"
  &#1111;"2"]=>
  string(int) "<img src="anotherimage.jpg">"
  &#1111;"3"]=>
  string(int) "<img src="andanotherimage.jpg">"
&#125;
This would need to work for any string of up to any reasonable length.
I've played around using strpos(), substr(), strstr() and all sorts, but I can't make anything do exactly what I want to.

I hope I have explained well enough.

Any help or pointers to get me going in the correct direction would be really great, thanks 8)

Posted: Mon Aug 11, 2003 10:01 am
by Wayne
Try looking at the preg and ereg regular expression functions, should be a good place to start.