Page 1 of 1

grab text from the following...

Posted: Sun Jul 15, 2007 6:08 am
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.

Posted: Sun Jul 15, 2007 6:32 am
by dxm
The best way is probably to use the strip_tags function.

http://uk3.php.net/strip_tags

Posted: Sun Jul 15, 2007 6:47 am
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.

Posted: Sun Jul 15, 2007 7:03 am
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.

Posted: Sun Jul 15, 2007 7:06 am
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.

Posted: Sun Jul 15, 2007 7:42 am
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?

Posted: Sun Jul 15, 2007 7:43 am
by feyd
Look in Useful Posts.

Posted: Sun Jul 15, 2007 8:06 am
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?

Posted: Sun Jul 15, 2007 8:10 am
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.

Posted: Sun Jul 15, 2007 8:20 am
by pedroz
I found it now... Thanks. Let me see if I can write a regular expression for this.

Posted: Sun Jul 15, 2007 8:33 am
by pedroz
Solved :)

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