Php get string between tags
If you must use a regular expression, the following will do the trick.
$str = 'foo {Vimeo}123456789{/Vimeo} bar';
preg_match('~{Vimeo}([^{]*){/Vimeo}~i', $str, $match);
var_dump($match[1]); // string(9) "123456789"
This may be more than what you want to go through, but here is a way to avoid regex.
$str = 'foo {Vimeo}123456789{/Vimeo} bar';
$m = substr($str, strpos($str, '{Vimeo}')+7);
$m = substr($m, 0, strpos($m, '{/Vimeo}'));
var_dump($m); // string(9) "123456789"
PHP get string between tag [Multiple tag in one string]
You can simply use preg_match_all
function of the PHP along with following regex
~\[id\](.*?)\[\/id\]~
like as
$my_string = "I am with a [id]123[/id] and [id]456[/id]";
preg_match_all("~\[id\](.*?)\[\/id\]~",$my_string,$m);
print_r($m[1]);
Get ALL content between two tags in a string
Here is a function I've written some months ago for exactly this issue. You'd have to call the function several times with the offset according to the returned $pos in order to get all accurances. I hope that's acceptable.
/* getSubstring
returns a substring between string_before and string_after,
starts to search at the offset. character of source
returns an array: ['text'] = string found
['pos'] = starting position of string_before within source
returns text = '' and pos = false if string is not found
example: $key = get_substring('12345here67890', '12345','67890')[0];
*/
function getSubstring($source, $string_before, $string_after, $offset = 0, $includeEnvelope = false) {
if (strlen($source)<$offset)
return(array('text' => '', 'pos' => false)); // kind of silly, but prevents an 'Offset not contained in string' warning
$pos1 = strpos($source, $string_before, $offset);
$sb_len = strlen($string_before);
if ($pos1===false) {
$result = '';
}
else {
$pos2 = strpos($source, $string_after, $pos1 + strlen($string_before));
if ($pos2===false) {
$result = '';
$pos1 = false;
}
else {
$result = substr($source, $pos1 + $sb_len, $pos2 - $pos1 - $sb_len);
if ($includeEnvelope)
$result = $string_before.$result.$string_after;
}
}
return (array('text' => $result, 'pos' => $pos1));
}
How do I get text containing a certain string between tags
Your pattern contains [^\n]*
that matches any 0+ chars other than newlines, and that makes the regex engine match across any &
chars greedily and find the last &
on the line.
You may use
'~&([^&]*?thistext[^&]*)&~'
Then, grab Group 1 value. See the regex demo.
Details
&
- a&
char([^&]*?thistext[^&]*)
- Capturing group 1:[^&]*?
- any 0+ chars other than&
, as few as possiblethistext
- literal text[^&]*
- any 0+ chars other than&
, as many as possible
&
- a&
char
PHP demo:
$str = 'thisip4:isatextexample&ineed.thistext&TXT:&andthis.idontneed&txt:&test.thistext&';
if (preg_match_all('~&([^&]*?thistext[^&]*)&~', $str, $m)) {
print_r($m[1]);
}
// => Array ( [0] => ineed.thistext [1] => test.thistext )
Regular expression to get string between tags with or without id attribute
You can simply use below regex
<li.*?>(.*?)<\/li>
Over here
`<li.*?>` here `(.*)` is to capture all attributes of `li` and `?` is to if no attributes is defined or not even space count also
As both has different li
structure
You can check it
Demo
Note: For
HTML/XML
parsing don't go for regex you can simply useDOMDocument
for same
How to get a substring between two strings in PHP?
If the strings are different (ie: [foo] & [/foo]), take a look at this post from Justin Cook.
I copy his code below:
function get_string_between($string, $start, $end){
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}
$fullstring = 'this is my [tag]dog[/tag]';
$parsed = get_string_between($fullstring, '[tag]', '[/tag]');
echo $parsed; // (result = dog)
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]);
Related Topics
How to Decode a Base64 String (Gif) into Image in PHP/Html
Replace the Last Comma with an & Sign
JSON Encode an Entire MySQL Result Set
Symfony: Change Database Dynamically
PHP MySQL Set Connection Timeout
How to Solve This "Warning: MySQLi_Connect(): (Hy000/1049): Unknown Database" Problem
How to Convert Special Characters to Normal Characters
Echo a Multi Dimensional Array
How to Keep Leading Zeros in PHP Integer
In Simplexml, How to Add an Existing Simplexmlelement as a Child Element
Including PHP Variables in an External Js File