PHP Regex find text between custom added HTML Tags
Assuming <PRODUCT_LIST>
tags will never be nested
preg_match_all('/<PRODUCT_LIST>(.*?)<\/PRODUCT_LIST>/s', $html, $matches);
//HTML array in $matches[1]
print_r($matches[1]);
How to get text between custom dynamic html tags without end tags
To get a text between regular expression matches you can use the preg_split
function:
$result = preg_split('/<\*[^|]+\|[^>]+>/', $input);
In this regular expression:
<\*
matches<*
;[^|]+
matches any symbol except|
1..* times;\|
matches|
;[^>]+
matches any symbol except>
1..* times;>
matches>
.
With this input:
$input = <<<EOL
<*fixedTagName|Dynamic part of tag name> // * and | are included in fixed part of tag name
//dynamic part can have spaces between words
Random text I need to get of unknown length
some paragraphs of text can start like this(look bellow)
» name: value
» name: value
<*fixedTagName|Dynamic part of tag>
More random text I need to get
<*fixedTagName|Dynamic part of tag>
Final part of random text I need to get
EOL;
The $result
will be an array of string something like that:
Array
(
[0] =>
[1] => // * and | are included in fixed part of tag name
//dynamic part can have spaces between words
Random text I need to get of unknown length
some paragraphs of text can start like this(look bellow)
» name: value
» name: value
[2] =>
More random text I need to get
[3] =>
Final part of random text I need to get
)
Regex select all text between tags
You can use "<pre>(.*?)</pre>"
, (replacing pre with whatever text you want) and extract the first group (for more specific instructions specify a language) but this assumes the simplistic notion that you have very simple and valid HTML.
As other commenters have suggested, if you're doing something complex, use a HTML parser.
How to get string between two constants using preg match in PHP
Your problem can be solved with this.
$content = 'blah [faq] blag blag blag [/faq] blah';
preg_match("%\[faq\](.*?)\[/faq\]%i", $content, $matches);
print_r($matches[1]);
We add one more match and get it index 1. Index 0 is full matched pattern and index 1 is second match from question mark in (.*?)
Use PHP to extract HTML code between special tags into an array
Use pretg_match_all() function to do this.
<?php
$html = '<<BEGIN>><div>Some text goes here...</div><<END>><<BEGIN>><table border="0"><tr><td>Table cell text goes here</td></tr></table><<END>><<BEGIN>><ul><li>My string</li><li>Another string</li></ul><<END>>';
preg_match_all("/<<BEGIN>>(.*)<<END>>/", $html, $result);
echo '<pre>';
print_r($result[1]);
echo '</pre>';
?>
Show the source code of the page and you will see all you wanted :) (, ...)
Related Topics
How to Install and Use PHP on Macos Via Brew
Setting Selected Option in Laravel Form
How to Remove Duplicate Rows in Laravel
PHP Code Is Not Being Executed, But the Code Shows in the Browser Source Code
How to Implement Basic "Long Polling"
Showing All Errors and Warnings
Laravel, Failed to Authenticate on Smtp Server With Username "" Using 3 Possible Authenticators
Convert Accented Characters to Their Plain Ascii Equivalents
How to Get Comma Separated Values from a Table in Laravel
PHP Mail Function Doesn't Complete Sending of E-Mail
Redirect That Same Page from Where Id Is Coming
How to Update Multiple Select Box Values in PHP MySQL
Reference - What Does This Symbol Mean in PHP
How to Remove Duplicate Values from a Multi-Dimensional Array in PHP
How Does PHP 'Foreach' Actually Work