I have a function which basically retrieves the product id in a description.
private function scanForProductIdInDescription($string, $start, $end) {
$startpos = strpos($string, $start) + strlen($start);
if (strpos($string, $start) !== false) {
$endpos = strpos($string, $end, $startpos);
if (strpos($string, $end, $startpos) !== false) {
return substr($string, $startpos, $endpos - $startpos);
}
}
}
i use it as follows:
$from = "{PID =";
$end = "}";
$description = 'some text {PID =340} {PID =357}';
$product_id = $this->scanForProductIdInDescription($description, $from, $end);
at the moment, it only gets the first occurence in the string. I need to find all occurences in the string. The result should be: $product_id = 340,357;
thanks
Using a regular expression instead of
strpos()
is going to be you best bet. I've quickly put the following together which works with your example;\{PID\s=([0-9]*)\}
You can see a working version here
Use of this in PHP would look like;
Edit: Edited to return only the actual ID in the matched string. IMO - this is a better solution than the other 2 answers posted as it returns ID's of any length, and only returns ID's matched in the format you've provided.
I've also updated my working example.