regex - PHP: Undefined offset: 3 - preg_match_all -


i have url structure example:

http://www.example.com/directory/some-text-a1-vs-sec-text-b2-vs-third-text-vs-last-text-c1/

my regex is:

preg_match_all("/([^\/.]+?)(?:-vs-|\.\w+$)/", $html, $matches); 

expected result:

some-text-a1 sec-text-b2 third-text last-text-c1 

result got:

some-text-a1 sec-text-b2 third-text notice: undefined offset: 3 in f:\xampp\htdocs\url.php on line 41 

full code:

$html = "http://www.example.com/directory/some-text-a1-vs-sec-text-b2-vs-third-text-vs-last-text-c1/"; preg_match_all("/([^\/.]+?)(?:-vs-|\.\w+$)/", $html, $matches);  $prvi = "some-text-a1"; $drugi = "sec-text-b2"; $treci = "third-text"; $cetvrti = "last-text-c1";  echo "url: ".$html."<br>";  if($prvi == $matches[1][0]){echo "1st o.k. - ".$prvi." = ".$matches[1][0]."<br>";} if($drugi == $matches[1][1]){echo "2nd o.k. - ".$drugi." = ".$matches[1][1]."<br>";} if($treci == $matches[1][2]){echo "3rd o.k. - ".$treci." = ".$matches[1][2]."<br>";} if($cetvrti == $matches[1][3]){echo "4th o.k. - ".$cetvrti." = ".$matches[1][3]."<br>";} 

ideas missing? suppose ending slash / problem within regex.

any ideas? thanks!

try this

(?<=vs-)(.*?)(?=-vs)|(?<=\/)([^\/]*?)(?=-vs)|(?<=vs-)(.*?)(?=\/|$) 

regex demo

explanation:
(?<=…): positive lookbehind sample
( … ): capturing group sample
.: character except line break sample
*: 0 or more times sample
?: once or none sample
(?=…): positive lookahead sample
|: alternation / or operand sample
\: escapes special character sample
[^x]: 1 character not x sample
$: end of string or end of line depending on multiline mode sample

php:

<?php $re = "/(?<=vs-)(.*?)(?=-vs)|(?<=\\/)([^\\/]*?)(?=-vs)|(?<=vs-)(.*?)(?=\\/|$)/"$ $str = "http://www.example.com/directory/some-text-a1-vs-sec-text-b2-vs-third-t$  preg_match_all($re, $str, $matches); print_r($matches[0]); 

output:

array (     [0] => some-text-a1     [1] => sec-text-b2     [2] => third-text     [3] => last-text-c1 ) 

Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -