簡體   English   中英

Preg_match某個URL-PHP

[英]Preg_match a certain URL - PHP

我有無數個看起來像這樣(1)的 URL,還有一些看起來像這樣(2)的 URL:

1) http://URL.com/gallery/image.png

2) http://URL.com/gallery/#/image.png

其中#代表編號的文件夾。

我想將thumb_添加到URL中,所以看起來像這樣:

1) http://URL.com/gallery/thumb_image.png

2) http://URL.com/gallery/#/thumb_image.png

我該怎么做?

要考慮的一件事是,圖像文件之前的文件夾可以是任何數字,並且圖像文件並不總是命名為“ image.png”。 我以這個為例。

您可以使用一個簡單的正則表達式,如下所示:

(\w+\.\w+)$

用替換字符串:

thumb_\1

工作演示

郵遞區號

$re = '/(\w+\.\w+$)/m';
$str = 'http://URL.com/gallery/image.png
http://URL.com/gallery/#/image.png';
$subst = 'thumb_\\1';

$result = preg_replace($re, $subst, $str);

echo "The result of the substitution is ".$result;

您可以使用preg_replace函數

<?PHP
$input_lines="http://URL.com/gallery/#/image.png";
ECHO preg_replace("/\w+\.\w+$/", "thumb_$0", $input_lines);
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM