Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从php URL字符串抓取变量_Php_String - Fatal编程技术网

从php URL字符串抓取变量

从php URL字符串抓取变量,php,string,Php,String,从字符串中获取6个字符id的最简单方法是什么 id将始终位于www.twitpic.com/之后,并且始终为6个字符 e.g., $string = 'The url is http://www.twitpic.com/f1462i. Enjoy.'; $id = 'f1462i'; 谢谢。给你。不带正则表达式的完整工作代码: preg_match("@twitpic\.com/(\w{6})@", "The url is http://www.twitpic.com/f1462

从字符串中获取6个字符id的最简单方法是什么

id将始终位于www.twitpic.com/之后,并且始终为6个字符

e.g., $string = 'The url is http://www.twitpic.com/f1462i.  Enjoy.';
      $id = 'f1462i';

谢谢。

给你。不带正则表达式的完整工作代码:

preg_match("@twitpic\.com/(\w{6})@", "The url is http://www.twitpic.com/f1462i.  Enjoy.", $m);
$id = $m[1];
<?php
$string = 'The url is http://www.twitpic.com/f1462i.  Enjoy.';
$id = substr($string, strpos($string, 'http://www.twitpic.com/')+23, 6);
echo $id;   //output: f1462i
?>

给你。不带正则表达式的完整工作代码:

<?php
$string = 'The url is http://www.twitpic.com/f1462i.  Enjoy.';
$id = substr($string, strpos($string, 'http://www.twitpic.com/')+23, 6);
echo $id;   //output: f1462i
?>


-1:如果可以避免使用正则表达式,就不要使用正则表达式,因为它们是性能杀手。@GregFire:当OP将问题标记为“正则表达式”时,你为什么要对答案投反对票?@Alan Moore:因为我没有读标记。。。如果可以的话,我会取消这个,但似乎太晚了。我重新标记了这个问题,正则表达式与此无关。-1:如果可以避免使用正则表达式,就不要使用它们,它们是性能杀手。@GregFire:当OP将问题标记为“正则表达式”时,你为什么要否决答案?@Alan Moore:因为我没有读标记。。。如果可以的话,我会取消这个,但似乎太晚了。我重新回答这个问题,正则表达式与此无关。