Who can help me think of a better way to use PHP to extract the contents of urlq= to &sa=u in the following string
urlq=https://zh.wikipedia.org/zh/test&sa=U&ved=0ahUKEwjZyO_jtvTKAhXFGh4KHVPkA70QFggeMAE&usg=AFQjCNET0LD_wKKA7nfCkcC44Oii6I13nQ
Reply content:
Who can help me think of a better way to use PHP to extract the contents of urlq= to &sa=u in the following string
urlq=https://zh.wikipedia.org/zh/test&sa=U&ved=0ahUKEwjZyO_jtvTKAhXFGh4KHVPkA70QFggeMAE&usg=AFQjCNET0LD_wKKA7nfCkcC44Oii6I13nQ
It's regular.
$str = 'urlq=https://zh.wikipedia.org/zh/test&sa=U&ved=0ahUKEwjZyO_jtvTKAhXFGh4KHVPkA70QFggeMAE&usg=AFQjCNET0LD_wKKA7nfCkcC44Oii6I13nQ';preg_match("/urlq=(.*?)&sa/", $str, $a);$b = $a[1];echo $b;
Split up.
$str = 'urlq=https://zh.wikipedia.org/zh/test&sa=U&ved=0ahUKEwjZyO_jtvTKAhXFGh4KHVPkA70QFggeMAE&usg=AFQjCNET0LD_wKKA7nfCkcC44Oii6I13nQ';$str1 = explode('urlq=', $str);$str2 = explode('&sa', $str1[1]);$b = $str2[0];echo $b;
See if this function of Parse_url
Give you a custom function
//$kw1为想截取的字符串//$mark1位起始字符//$mark2为结束字符//返回一个截取好的字符串//截取指定两个字符串之间function getNeedBetween($kw1,$mark1,$mark2){ $kw=$kw1; $kw='123'.$kw.'123'; $st =stripos($kw,$mark1); $ed =stripos($kw,$mark2); if(($st==false||$ed==false)||$st>=$ed) return 0; $kw=substr($kw,($st+1),($ed-$st-1)); return $kw;}