Ben bu biçimde bir dize var:
/SV/temp_images/766321929_2.jpg
Çizgi ÖNCE numaralarını almak için kod herhangi bir küçük parça var, ANCAK temp_images SONRA /? Bu durumda ben ... 766321929 sadece almak istiyor?
Teşekkürler
Bu hile yapmak gerekir:
$str = '/SV/temp_images/766321929_2.jpg';
$m = array();
if (preg_match('#temp_images/(\d+)_#', $str, $m)) {
echo $m[1];
}
The idea :
preg_match
with a regular expression
temp_images/
' ile başlayan şey ile çalışır_
' ile biter\d
ile sembolize) numaralarını döndürür$m
, will contain what was matched :
$m[0]
eşleşti her şey olacak$m[1]
(between ()
) em> bölüm çekilen ilk olacak
And the output :
766321929
And if you're curious and what to know more about regex, don't hesitate to take a look at Regular Expressions (Perl-Compatible) -- there is a lot of interesting stuff to read, there ;-)
Burada kirli bir yoldur:
$contents = ' /SV/temp_images/766321929_2.jpg';
$contents = substr($contents, strpos($contents, 'temp_images/'));
$contents = substr($contents, strpos($contents, '/'));
$contents = substr($contents, 1);
$contents = substr($contents, 0, strpos($contents, '_'));
print $contents;