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
pedroz
Forum Commoner
Posts: 99 Joined: Thu Nov 03, 2005 6:21 am
Post
by pedroz » Sun Jul 15, 2007 6:08 am
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.
dxm
Forum Newbie
Posts: 8 Joined: Fri Jun 29, 2007 5:18 pm
Location: Wales
Post
by dxm » Sun Jul 15, 2007 6:32 am
The best way is probably to use the
strip_tags function.
http://uk3.php.net/strip_tags
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Jul 15, 2007 6:47 am
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.
dxm
Forum Newbie
Posts: 8 Joined: Fri Jun 29, 2007 5:18 pm
Location: Wales
Post
by dxm » Sun Jul 15, 2007 7:03 am
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:
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Jul 15, 2007 7:06 am
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 » Sun Jul 15, 2007 7:42 am
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?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Jul 15, 2007 7:43 am
Look in Useful Posts.
pedroz
Forum Commoner
Posts: 99 Joined: Thu Nov 03, 2005 6:21 am
Post
by pedroz » Sun Jul 15, 2007 8:06 am
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?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sun Jul 15, 2007 8:10 am
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 » Sun Jul 15, 2007 8:20 am
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 » Sun Jul 15, 2007 8:33 am
Solved
preg_match('#<big><a href="(.+?)">(.+?)</a></big>#i',$source,$matches);
echo $matches[2];