Hello
I have a string that be a simple as "Hello"
or may be embedded as value inside an html tags like so:
"<b><span ...>Hello</span></b>"
I need some way to extract the "Hello" part from the string
Is there any HTML processing function in PHP that can help me here?
regards
extracting value from an HTML tag
Moderator: General Moderators
-
malcolmboston
- DevNet Resident
- Posts: 1826
- Joined: Tue Nov 18, 2003 1:09 pm
- Location: Middlesbrough, UK
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Code: Select all
preg_match('|<(.+)>(.+?)</\\1>|',$data,$matches);In the following:
Code: Select all
<b><i>text</i></b>So, if you just want to strip the tags,
Code: Select all
$data = preg_replace('|<[^>]+>|','',$data);- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
You can indeed use \\1 within the match itself. Can I just say however, that due to the nature of escaping numeric sequences it is always now recommended to use $1 other than how it is used here inside the regex.
Another point; Don't delimit the regex with "|" - use # instead. The BAR ( "|" ) character is a regex operator so it's a nasty habit to get into.
There's a problem with the regex you write however anyway.
Take for example:
Now, your regex is looking for
Do this instead....
Another point; Don't delimit the regex with "|" - use # instead. The BAR ( "|" ) character is a regex operator so it's a nasty habit to get into.
There's a problem with the regex you write however anyway.
Take for example:
Code: Select all
<span style="e;color:red"e;>Hello world</span>Code: Select all
<span style="e;color:red"e;>Hello world</span style="e;color:red"e;>Code: Select all
preg_match('#<(\w+)[^>]*>(.+?)</\\1>#s', $data, $matches);
print_r($matches);