Get string between two strings
My string is: «reply-234-private», i want to get the number after «reply-» and before «-private», it is «234». I have tried with following code but it returns an empty result:
$string = 'reply-234-private'; $display = preg_replace('/reply-(.*?)-private/','',$string); echo $display;
5 Answers 5
string(5) "reply" [1]=> string(3) "234" [2]=> string(7) "private" > echo $display[1]; // prints 234
perfect sir works very well, here is code i used to get gdrive uid preg_match ( ‘/id=(.*?)&export/’ , $gdrive2 , $matches );
$myString = 'reply-234-private'; $myStringPartsArray = explode("-", $myString); $answer = $myStringPartsArray[1];
This article show you, How to get of everything string between two tags or two strings.
in case your question, you can try this :
$string1='reply-234-private'; echo GetStringBetween ($string1, "-", "-")
or we can use any ‘identifier string’ for grab the string between the identifier string. for example:
echo GetStringBetween ($string1, "reply-", "-private")
Use php’s inbuilt regex support functions preg_match_all
this would help, suppose you want the array of strings(keys) between @@ in following example, where ‘/’ doesn’t fall in-between, you can built new example with different start an end variable
function getInbetweenStrings($start, $end, $str) < $matches = array(); $regex = "/$start([a-zA-Z0-9_]*)$end/"; preg_match_all($regex, $str, $matches); return $matches[1]; >$str = "C://@@ad_custom_attr1@@/@@upn@@/@@samaccountname@@"; $str_arr = getInbetweenStrings('@@', '@@', $str); print_r($str_arr);
Get content between two strings PHP
For some reason this appears to work on one place in my code and not another. Am I going about this in the right way? Or is there a better way? Also is output buffer the way to do this or file_get_contents? Thanks in advance!
If it works in some situations and not others, you should provide examples of when it works and when it does not.
6 Answers 6
You may as well use substr and strpos for this.
$startsAt = strpos($out, "") + strlen(""); $endsAt = strpos($out, " ", $startsAt); $result = substr($out, $startsAt, $endsAt - $startsAt);
You’ll need to add error checking to handle the case where it doesn’t FINDME.
@472084 yes but you can write a wrapper function which performs this code in a while loop or recursively. This answer is a very good base.
- Use # instead of / so you dont have to escape them.
- The modifier s allows . to match newlines.
- < may be the start of a or quantifier. The closing > has no special meaning, but escaping it doesn’t cause an error.
- The basic
$delimiter = '#'; $startTag = ''; $endTag = ' '; $regex = $delimiter . preg_quote($startTag, $delimiter) . '(.*?)' . preg_quote($endTag, $delimiter) . $delimiter . 's'; preg_match($regex,$out,$matches);
Put this code in a function
- For any file which you do not want to execue any stray php code, you should use file_get_contents. include/require should not even be an option there.
This is a good solution. By adding the U (ungreedy) modifier (#sU) it’s possible to use multiple instances of the same search tags.
I like to avoid using regex if possible, here is alternative solution to fetch all strings between two strings and returns an array.
function getBetween($content, $start, $end) < $n = explode($start, $content); $result = Array(); foreach ($n as $val) < $pos = strpos($val, $end); if ($pos !== false) < $result[] = substr($val, 0, $pos); >> return $result; > print_r(getBetween("The quick brown > jumps over the lazy >", ">"));
I love these two solutions
function GetBetween($content,$start,$end) < $r = explode($start, $content); if (isset($r[1]))< $r = explode($end, $r[1]); return $r[0]; >return ''; > function get_string_between($string, $start, $end)
I also made few benchmarks as well with both solutions above and both are giving almost the same time. You can test it as well. I gave both functions a file to read which had about 60000 characters (reviewed with Ms. Word’s word count) and both functions resulted in about 0.000999 seconds to find.
$startTime = microtime(true); GetBetween($str, '', ''); echo "Explodin Function took: ".(microtime(true) - $startTime) . " to finish
"; $startTime = microtime(true); get_string_between($str, '', ''); echo "Subsring Function took: ".(microtime(true) - $startTime) . " to finish
";
Get string between — Find all occurrences PHP
I found this function which finds data between two strings of text, html or whatever. How can it be changed so it will find all occurrences? Every data between every occurrence of $start [some-random-data] $end. I want all the [some-random-data] of the document (It will always be different data).
function getStringBetween($string, $start, $end)
6 Answers 6
function getContents($str, $startDelimiter, $endDelimiter) < $contents = array(); $startDelimiterLength = strlen($startDelimiter); $endDelimiterLength = strlen($endDelimiter); $startFrom = $contentStart = $contentEnd = 0; while (false !== ($contentStart = strpos($str, $startDelimiter, $startFrom))) < $contentStart += $startDelimiterLength; $contentEnd = strpos($str, $endDelimiter, $contentStart); if (false === $contentEnd) < break; >$contents[] = substr($str, $contentStart, $contentEnd - $contentStart); $startFrom = $contentEnd + $endDelimiterLength; > return $contents; >
$sample = 'OneaaaTwoTwoThreeFourFive'; print_r( getContents($sample, '', '') ); /* Array ( [0] => One [1] => TwoTwo [2] => Four [3] => Five ) */
Can someone explain to me what searching algorithm was used to implement this? And can someone explain to me in words what this function precisely does?
This works flawlessly just the way you wrote it. Thank you so much! It finds every occurrence. For example, if you are looking for the text between two html tags, get the html page as a string. Then set $startDelimiter to the first tag and $endDelimiter to the second tag. The function will return every instance of what is between the two tags in an array.
@Erdss4 At each iteration a loop first collects a new position of $startDelimiter, then a position of $endDelimiter. The lookups always start from the position left by previous loop. That’s the general description, for details ask a specific more question. )
You can do this using regex:
function getStringsBetween($string, $start, $end) < $pattern = sprintf( '/%s(.*?)%s/', preg_quote($start), preg_quote($end) ); preg_match_all($pattern, $string, $matches); return $matches[1]; >
this is good for standard strings but bad for things like using dollar signs and carrots as delimiters because it messes up regex
for some reason, preg_match_all and preg_match_all don’t work on large strings, I tried to get all from a table.
I love to use explode to get string between two string. this function also works for multiple occurrences.
function GetIn($str,$start,$end) < $p1 = explode($start,$str); for($i=1;$ireturn $p; >
I needed to find all these occurences between specific first and last tag and change them somehow and get back changed string.
So I added this small code to raina77ow approach after the function.
$sample = 'One aaa TwoTwo Three Four aaaaa Five'; $sample_temp = getContents($sample, '', ''); $i = 1; foreach($sample_temp as $value) < $value2 = $value.'-'.$i; //there you can change the variable $sample=str_replace(''.$value.'',$value2,$sample); $i = ++$i; > echo $sample;
Now output sample has deleted tags and all strings between them has added number like this:
One-1 aaa TwoTwo-2 Three Four-3 aaaaa Five-4
But you can do whatever else with them. Maybe could be helpful for someone.
There was some great sollutions here, however not perfekt for extracting parts of code from say HTML which was my problem right now, as I need to get script blocks out of the HTML before compressing the HTML. So building on @raina77ow original sollution, expanded by @Cas Tuyn I get this one:
$test_strings = [ '0a
1b
2c
3', '0a
1b
2c
', 'a
1b
2c
3', 'a
1b
2c
', ' 1b' ]; /** * Seperate a block of code by sub blocks. Example, removing all . tags from HTML kode * * @param string $str, text block * @param string $startDelimiter, string to match for start of block to be extracted * @param string $endDelimiter, string to match for ending the block to be extracted * @return array [all full blocks, whats left of string] */ function getDelimitedStrings($str, $startDelimiter, $endDelimiter) < $contents = array(); $startDelimiterLength = strlen($startDelimiter); $endDelimiterLength = strlen($endDelimiter); $startFrom = $contentStart = $contentEnd = $outStart = $outEnd = 0; while (false !== ($contentStart = strpos($str, $startDelimiter, $startFrom))) < $contentStart += $startDelimiterLength; $contentEnd = strpos($str, $endDelimiter, $contentStart); $outEnd = $contentStart - 1; if (false === $contentEnd) < break; >$contents['in'][] = substr($str, ($contentStart-$startDelimiterLength), ($contentEnd + ($startDelimiterLength*2) +1) - $contentStart); if( $outStart ) < $contents['out'][] = substr($str, ($outStart+$startDelimiterLength+1), $outEnd - $outStart - ($startDelimiterLength*2)); >else if( ($outEnd - $outStart - ($startDelimiterLength-1)) > 0 ) < $contents['out'][] = substr($str, $outStart, $outEnd - $outStart - ($startDelimiterLength-1)); >$startFrom = $contentEnd + $endDelimiterLength; $startFrom = $contentEnd; $outStart = $startFrom; > $total_length = strlen($str); $current_position = $outStart + $startDelimiterLength + 1; if( $current_position < $total_length ) $contents['out'][] = substr($str, $current_position); return $contents; >foreach($test_strings AS $string)< var_dump( getDelimitedStrings($string, '
', '
') ); >
wlements with the possible innerHTML aswell, giving this result:
array (size=2) 'in' => array (size=3) 0 => string 'a
' (length=8) 1 => string 'b
' (length=8) 2 => string 'c
' (length=8) 'out' => array (size=4) 0 => string '0' (length=1) 1 => string '1' (length=1) 2 => string '2' (length=1) 3 => string '3' (length=1) array (size=2) 'in' => array (size=3) 0 => string 'a
' (length=8) 1 => string 'b
' (length=8) 2 => string 'c
' (length=8) 'out' => array (size=3) 0 => string '0' (length=1) 1 => string '1' (length=1) 2 => string '2' (length=1) array (size=2) 'in' => array (size=3) 0 => string 'a
' (length=8) 1 => string 'b
' (length=8) 2 => string 'c
' (length=8) 'out' => array (size=3) 0 => string '1' (length=1) 1 => string '2' (length=1) 2 => string '3' (length=1) array (size=2) 'in' => array (size=3) 0 => string 'a
' (length=8) 1 => string 'b
' (length=8) 2 => string 'c
' (length=8) 'out' => array (size=2) 0 => string '1' (length=1) 1 => string '2' (length=1) array (size=2) 'in' => array (size=1) 0 => string ' ' (length=7) 'out' => array (size=1) 0 => string '1b' (length=5)
PHP — text between tags
Don’t use regular expressions to parse HTML. Use a proper HTML parsing module. You cannot reliably parse HTML with regular expressions, and you will face sorrow and frustration down the road. As soon as the HTML changes from your expectations, your code will be broken. See htmlparsing.com/php or this SO thread for examples of how to properly parse HTML with PHP modules that have already been written, tested and debugged.
2 Answers 2
Don’t use a regexp. Use an HTML parser!
Here’s an example with PHP Simple HTML DOM Parser, but you can do it with what you prefer:
$html = str_get_html('test
'); $div = $html->first_child(); // Here's the div $result = ""; for($children = $div->first_child; $children; $children = $children->next_sibling()) < $result += $children; >echo $result; // => "test
"
@cyrbil You can check here the reference. To achieve what you want you can do something like: $subling.»» == $sibling->plaintext
Ok, simpleDomParser’s documentation was a bit deprecated. But i manage to get through. Also that it doesn’t have the ability to check for text i manage to do it by walking recursivly into the dom, and watching if an element contain a piece of text not between tags. To check i remove every tags with a regex (as i also want to remove tag content, strip_tags isn’t enough) then check if i am left with some text.
For the record here is the complete code. Some regex may not be necessary in some cases. But i needed them all 😉
childNodes() as $child) < // get sub children $children = $child->childNodes(); // get inner content $content = $child->innertext; // remove starting and ending self closing elements or smarty tags $content = preg_replace('/(^(\s*<[^>]*?\/\s*>)+)|((<[^>]*?\/\s*>\s*)+$)/s', '', $content); $content = preg_replace('/(^(\s*<[^>]*?>)+)|((\<[^>]*?\>\s*)+$)/s', '', $content); $content = trim($content); // remove all elements and smarty tags $text = preg_replace('/<(\w+)[^>]*>.*/', '', $content); // remove elements $text = preg_replace('//', '', $text); // remove self closing elements $text = preg_replace('/\<.*?\>/', '', $text); // remove smarty tags $text = preg_replace('/[^\w]/', '', $text); // remove non alphanum characters $text = trim($text); // no children, we are at a leaf and it's probably a text if(empty($children)) < // check if not empty string and exclude comments styles and scripts if(!empty($text) && in_array($child->tag, array("comment","style","script")) === false) < // add to results $results[] = $content; >> // if we are on a branch but in contain text not inside tags elseif(!empty($text)) < // add to results $results[] = $content; >else < // recursive call with sub element parse($child, $results); >> >