grab text from the following...

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
pedroz
Forum Commoner
Posts: 99
Joined: Thu Nov 03, 2005 6:21 am

grab text from the following...

Post by pedroz »

I have the following code

<big><a href="http://www.url.com">text1</a></big>

I would like to have a function to grab "text1", note http://www.url.com is not the same and change every time.

Thanks.
User avatar
dxm
Forum Newbie
Posts: 8
Joined: Fri Jun 29, 2007 5:18 pm
Location: Wales

Post by dxm »

The best way is probably to use the strip_tags function.

http://uk3.php.net/strip_tags
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

strip_tags() isn't a very smart function, so it may mangle code. Do some searching of the forums about strip_tags. You may find other smarter functions that have been provided.
User avatar
dxm
Forum Newbie
Posts: 8
Joined: Fri Jun 29, 2007 5:18 pm
Location: Wales

Post by dxm »

It doesn't need to be smart so long as the format of the link is always <big><a href="http://www.url.com">text1</a></big>.

Code: Select all

echo strip_tags('<big><a href="http://www.url.com">text1</a></big>');
outputs:

Code: Select all

text1
If the format was unpredictable then I would look into something else, but for a simple snippet of HTML like yours, strip_tags() is ample.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If a tag has a ">" in it, strip_tags() will stop at that point, not the actual end of the tag. This sort of thing happens quite often when Javascript is incorporated in tags, which a lot of people still do.
pedroz
Forum Commoner
Posts: 99
Joined: Thu Nov 03, 2005 6:21 am

Post by pedroz »

Thanks for your support... It works.

But I was wondering using it with a preg_match function. My problem is creating a regular expression to grab the text1... Do you think it is possible? What regular expression should I use?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Look in Useful Posts.
pedroz
Forum Commoner
Posts: 99
Joined: Thu Nov 03, 2005 6:21 am

Post by pedroz »

something like this

preg_match('??????????',
"<big><a href="http://www.url.com">text1</a></big>", $matches);

the only problem is the regular expression. I am confused with it because I never understood the regular expressions...

Do you have any useful link in order to read and build the regular expression for this?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Did you look in Useful Posts? It's a sticky.

If you don't want to then look through the stickies (and threads) in the Regex board.
pedroz
Forum Commoner
Posts: 99
Joined: Thu Nov 03, 2005 6:21 am

Post by pedroz »

I found it now... Thanks. Let me see if I can write a regular expression for this.
pedroz
Forum Commoner
Posts: 99
Joined: Thu Nov 03, 2005 6:21 am

Post by pedroz »

Solved :)

preg_match('#<big><a href="(.+?)">(.+?)</a></big>#i',$source,$matches);
echo $matches[2];
Post Reply