Removing instances of text from strings

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
RTT
Forum Commoner
Posts: 38
Joined: Thu Jul 17, 2003 10:22 am
Location: Wolverhampton, UK
Contact:

Removing instances of text from strings

Post 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)
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

Try looking at the preg and ereg regular expression functions, should be a good place to start.
Post Reply