Php Regex Find Text Between Custom Added HTML Tags

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



Leave a reply



Submit