javascript-php extracts the middle part of the string

Source: Internet
Author: User
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;}
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.