Home > Backend Development > PHP Tutorial > javascript - php提取这段字符串中间的一段内容

javascript - php提取这段字符串中间的一段内容

WBOY
Release: 2016-06-06 20:18:30
Original
1387 people have browsed it

谁帮我想个好点的办法,用php提取下面的字符串中urlq=到&sa=U之间的内容

<code>urlq=https://zh.wikipedia.org/zh/test&sa=U&ved=0ahUKEwjZyO_jtvTKAhXFGh4KHVPkA70QFggeMAE&usg=AFQjCNET0LD_wKKA7nfCkcC44Oii6I13nQ
</code>
Copy after login
Copy after login

回复内容:

谁帮我想个好点的办法,用php提取下面的字符串中urlq=到&sa=U之间的内容

<code>urlq=https://zh.wikipedia.org/zh/test&sa=U&ved=0ahUKEwjZyO_jtvTKAhXFGh4KHVPkA70QFggeMAE&usg=AFQjCNET0LD_wKKA7nfCkcC44Oii6I13nQ
</code>
Copy after login
Copy after login

正则啦

<code>$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;</code>
Copy after login

分割啦

<code>$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;</code>
Copy after login

看看 parse_url 这个函数可不可以

给你一个自定义函数

<code>//$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;
}</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template