Jump to content

Select All In Between


spires

Recommended Posts

Hi

 

I'm pulling my data from a database.

I'm trying to grab the first <a> and place it in a String.

 

Any ideas?

 

 

<a> Tag:

 

<a href="http://lwww.mysite,com"><img alt="" src="/media/lci_strip.png" style="width: 701px; height: 92px; " /></a>

 

 

What I have so far - but not working:

 

$findImg = preg_match_all('/<a[^</a>]+</a>/i',$cleanMeg, $result);

$stripFirstImg = str_replace($result[0][0], "", $cleanMeg);

 

 

 

Thanks :)

Link to comment
https://forums.phpfreaks.com/topic/269343-select-all-in-between/
Share on other sites

That RegExp won't do quite what you expect it to, I'm afraid. It'll select everything between the first opening anchor tag, to the last closing anchor tag. So if start with one link, and finish with another, you'll select the entire text.

 

To get what you want you'll have to use this:

$RegExp = '#<a[^>]*>[^<]+</a>#i';

 

The square brackets define a character group, and since I've start them with a caret (^) I've told the RegExp engine that I want everything except the characters in the group. Which is why your first RegExp didn't work, since you only selected up to the first character that matched one of the characters inside the group.

To get your first RegExp to work, you would have had to use negative lookahead.

 

www.regular-expressions.info has more information on Regular Expressions, and well worth a read.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.