Reputation: 80
I am a beginner of PHP. I have string:
$fullstring = "this is [tag]dog[/tag], [tag]cat[/tag], [tag]lion[/tag]";
I want to get string "cat"
from my $fullstring
.
I used to try with this:
Get substring between two strings PHP
But I can only get the first string (dog)
. Thank you for your time.
Thet Cartter.
Upvotes: 1
Views: 982
Reputation: 80
I try to modify the old function [http://www.justin-cook.com/wp/2006/03/31/php-parse-a-string-between-two-strings/]. I got this:
function get_string_between($string, $start, $end, $index){
if ($index <= 0) return '';
$string = ' ' . $string;
$ini = 0;
$x = 1;
while ($x <= $index) {
$ini = strpos($string, $start, $ini + 1);
if ($ini == 0) return '';
$x++;
}
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}
$fullstring = "this is [tag]dog[/tag], [tag]cat[/tag], [tag]lion[/tag]";
echo get_string_between($fullstring, "[tag]", "[/tag]", 2); // Result = cat
Upvotes: 0
Reputation: 1015
Disclaimer: Both solution is naive. You should always check the returned value of preg_match_all
and strpos
.
Try to use preg_match_all
$fullstring = "this is [tag]dog[/tag], [tag]cat[/tag], [tag]lion[/tag]";
preg_match_all("'\[tag\](.*?)\[\/tag\]'si", $fullstring, $match);
foreach($match[1] as $val){
echo $val, ' ';
}
// result: dog cat lion
To get only the content of the second tag (which is cat now)
echo $match[1][1]
// result: cat
$fullstring = 'this is [tag]dog[/tag], [tag]cat[/tag], [tag]lion[/tag]';
function get_strings_between(string $string, string $start, string $end): array {
$r = [];
$startLen = strlen($start);
$endLen = strlen($end);
while (!empty($string)) {
$startPosition = strpos($string, $start);
if ($startPosition === false) {
return $r;
}
$contentStart = $startPosition + $startLen;
$contentEnd = strpos($string, $end, $contentStart);
$len = $contentEnd - $contentStart;
$r[] = substr($string, $contentStart, $len);
$endPosition = $contentEnd + $endLen;
$string = substr($string, $endPosition);
}
return $r;
}
$match = get_strings_between($fullstring, '[tag]', '[/tag]');
// result: dog cat lion
To get only the content of the second tag (which is cat now)
echo $match[1]
// result: cat
Upvotes: 5
Reputation: 660
You're looking for preg_match()
function. An example below may help you out.
$fullString = "this is [tag]dog[/tag], [tag]cat[/tag], [tag]lion[/tag]";
$regex = "/\[tag\]\w+\[\\tag\]/";
preg_match($regex, $fullString, $matches);
foreach($matches as $match){
echo $match;
}
// result, dog, cat, lion
Upvotes: 0
Reputation: 78
This is a modified version of this answer. This function will return all instances it can find based on what you want to search.
function getContents($str, $startDelimiter, $endDelimiter, $needle) {
$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;
}
$tempString = substr($str, $contentStart, $contentEnd - $contentStart);
if ($tempString == $needle) {
$contents[] = $tempString;
}
$startFrom = $contentEnd + $endDelimiterLength;
}
return $contents;
}
Upvotes: 0