Preg match text in php between html tags
preg_match("'<p class=\"review\">(.*?)</p>'si", $source, $match);
if($match) echo "result=".$match[1];
preg_match - text between closing and opening tag
Looks like something wrong with your php installation/configuration.
Your code as it's.
$content = '<h2>Title of post</h2> 1 category <strong>task 1</strong> 1 category <strong>task 2</strong> 1 category <strong>task 3</strong> ';
preg_match_all('#<\/h2>(.*?)<strong>#',$content,$matches);
print_r($matches);
Output:
Array
(
[0] => Array
(
[0] => </h2> 1 category <strong>
)
[1] => Array
(
[0] => 1 category
)
)
Live demo
Note: Since there is only one match of your pattern ( between </h2>
<strong>
) you can access like $maches[1][0]
or use preg_match
.
Using preg_match to capture text between tags with exception with PHP
This is just a bit complicated, for which we'd just define two expressions and join them using a logical OR |
:
<span class="place ville">Ville : <span><.+?>(.+?)<\/
and
<span class="place ville">Ville : <span>([^<]+)?<
RegEx
<span class="place ville">Ville : <span><.+?>(.+?)<\/|<span class="place ville">Ville : <span>([^<]+)?<
Demo
Test
$re = '/<span class="place ville">Ville : <span><.+?>(.+?)<\/|<span class="place ville">Ville : <span>([^<]+)?</m';
$str = '<span class="place ville">Ville : <span>City name</span></span>
<span class="place ville">Ville : <span><a href="https://example.com">City name</a></span></span>
<span class="place ville">Ville : <span>Århus</span></span>
<span class="place ville">Ville : <span><a href="https://example.com">City name</a></span></span>
';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
foreach ($matches as $key => $city) {
if ($city[1] == "") {
echo $city[2] . "\n";
} else {
echo $city[1] . "\n";
}
}
Output
City name
City name
Århus
City name
preg match text between tags excluding same tag in between
The main problem is that you did not use a word boundary after the opening tag and thus, namespace
in the pattern could also match namespaces
tag, and many others.
The subsequent issue is that the <${tag}\b[^>]*>(.*?)<\/${tag}>
pattern would overfire if there is a self-closing namespace
tag followed with a "normal" paired open/close namespace
tag. So, you need to either use a negative lookbehind (?<!\/)
before the >
(see demo), or use a (?![^>]*\/>)
negative lookahead after \b
(see demo).
So, you can use
$tag_ini = "<{$tag}\\b[^>]*(?<!\\/)>"; $tag_end = "<\\/{$tag}>";
preg_match easiest way to match text from inside html tags
I normally suggest if you need to actually express what you're looking for in a HTML document to use an xpath
expression for that because it can give you the actual value whereas regex'es are not able to further parse the HTML/XML, and xpath
expressions are much more fine-grained. See the output which returns the text-value for example w/o any further tags inside:
array(8) {
[0]=>
string(16) "Text text text:3"
[1]=>
string(14) "Text text text"
[2]=>
string(1) "1"
[3]=>
string(1) "0"
[4]=>
string(1) "2"
[5]=>
string(4) "2.90"
[6]=>
string(4) "3.20"
[7]=>
string(4) "1.85"
}
Code:
$html = <<<EOD
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="rowData">
<tr align="center" class="fnt-vrdana-mavi" >
<td style="font-size:11px" colspan=3><b>Text text text</b>:3</td>
</tr>
<tr class="header" align="center">
<td height="18" colspan="3">Text text text</td>
</tr>
<tr align="center" class="fnt-vrdana" bgcolor="#eff3f4" height="18">
<td width="32%" height="17"><b>1</b></td>
<td width="34%"><b>0</b></td>
<td width="34%"><b>2</b></td>
</tr>
<tr align="center" class="fnt-vrdana-mavi">
<td height="17">2.90</td>
<td>3.20</td>
<td>1.85</td>
</tr>
</table>
EOD;
// create DomDocument to operate xpath on
$doc = new DomDocument;
$doc->loadHTML($html);
// create DomXPath
$xpath = new DomXPath($doc);
// perform the XPath query
$nodes = $xpath->query('//td');
// process nodes to return their actual value
$values = array();
foreach($nodes as $node) {
$values[] = $node->nodeValue;
}
var_dump($values);
Related Topics
Compile a Referenced Less File into CSS with PHP Automatically
How to Force PHP to Use Strings for Array Keys
How to Remove the Url Protocol and Slash from User Input in PHP
Pdo Fetchall Array to One Dimensional
How to Delete Files from Directory Based on Creation Date in PHP
How to Decrypt a Password Hash in PHP
Codeigniter PHP Model Access "Unable to Locate the Model You Have Specified"
Executing a PHP Script with a Cron Job
Set Session in Database in PHP
Why Does PHP Convert a String with the Letter E into a Number
Php.Ini Changes, But Not Effective on Ubuntu
Apache Error [Notice] Parent: Child Process Exited with Status 3221225477 -- Restarting
Regex to Strip Comments and Multi-Line Comments and Empty Lines
Long Polling/Http Streaming General Questions