String in one tag?
Moderator: General Moderators
- MarK (CZ)
- Forum Contributor
- Posts: 239
- Joined: Tue Apr 13, 2004 12:51 am
- Location: Prague (CZ) / Vienna (A)
- Contact:
String in one tag?
Let's say I have an unknown string. Now, I want to find out, whether it's in one tag or not, like this:
"<b>This is bold text</b>"
but not like this:
"<b>This is bold text</b> and this is not"
or
"<b>This is bold text</b><i>and this is not</i>"
How should I find out? Regex?
"<b>This is bold text</b>"
but not like this:
"<b>This is bold text</b> and this is not"
or
"<b>This is bold text</b><i>and this is not</i>"
How should I find out? Regex?
- MarK (CZ)
- Forum Contributor
- Posts: 239
- Joined: Tue Apr 13, 2004 12:51 am
- Location: Prague (CZ) / Vienna (A)
- Contact:
I'm still not sure how.
I have this:
but that doesn't fix a string like this:
"<b>This is bold text</b> and <b>this is bold too</b>"
I have this:
Code: Select all
if (mb_ereg("^<([a-zA-Z]+).+</(.+)>$", $item, $parts) &&
$parts[1] == $parts[2])"<b>This is bold text</b> and <b>this is bold too</b>"
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
The really basic, fairly dumb, PCRE version:
Code: Select all
#^<\s*([a-zA-Z]+)[^>]*>.*?<\s*\\1[^>]*>$#- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
The tags must match.
I forgot a tiny bit:
I forgot a tiny bit:
Code: Select all
<?php
$pattern = '#^<\s*([a-zA-Z]+)[^>]*>.*?<\s*/\s*\\1[^>]*>$#s';
$tests = array(
'<b>This is bold text</b>' => true,
'<b>This is bold text</b> and this is not' => false,
'<b>This is bold text</b><i>and this is not</i>' => false,
);
$results = array();
foreach($tests as $test => $result)
{
$results[] = (preg_match($pattern, $test) == $result);
}
if (count(array_filter($results)) == count($results))
{
echo 'it works.';
}
else
{
echo 'it doesn\'t work.';
var_dump($results);
}
?>- MarK (CZ)
- Forum Contributor
- Posts: 239
- Joined: Tue Apr 13, 2004 12:51 am
- Location: Prague (CZ) / Vienna (A)
- Contact:
That is similar to what I've done...
However, your still fails for these:
First one is just forgeting about numbers in tags (h1-h6)
but for the second one, mine fails too. I would probably have to parse it as xml and than test it like that, I don't see any way how to fix that via regex.
However, your still fails for these:
Code: Select all
'<h1>This is bold text</h2>' => false,
'<b>This is bold text</b><b>and this is not</b>' => false,but for the second one, mine fails too. I would probably have to parse it as xml and than test it like that, I don't see any way how to fix that via regex.