從字符串中獲取6個字符的id最簡單的方法是什麼?從php中獲取變量的URL字符串
該ID始終在www.twitpic.com/後面,並且始終爲6個字符。
e.g., $string = 'The url is http://www.twitpic.com/f1462i. Enjoy.';
$id = 'f1462i';
謝謝。
從字符串中獲取6個字符的id最簡單的方法是什麼?從php中獲取變量的URL字符串
該ID始終在www.twitpic.com/後面,並且始終爲6個字符。
e.g., $string = 'The url is http://www.twitpic.com/f1462i. Enjoy.';
$id = 'f1462i';
謝謝。
$string = "http://www.twitpic.com/f1462i" ;
$id = substr($string,strpos($string, 'twitpic.com')+strlen('twitpic.com')+1,6) ;
echo $id ;
preg_match("@twitpic\.com/(\w{6})@", "The url is http://www.twitpic.com/f1462i. Enjoy.", $m);
$id = $m[1];
你在這裏。沒有regex
完整的工作代碼:
<?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:不要使用正則表達式的時候可以避開他們,他們是一個性能殺手。 – greg0ire 2010-08-22 18:37:43
@ greg0ire:爲什麼你在標記「正則表達式」問題的OP中爲什麼會低估*答案*? – 2010-08-23 04:08:49
@Alan Moore:因爲我沒有閱讀標籤......如果可以的話,我會取消這個標籤,但似乎爲時已晚。我重申了這個問題,正則表達式與此無關。 – greg0ire 2010-08-23 08:07:47