簡體   English   中英

如何使用正則表達式提取字符串

[英]How to extract string with regex

我正在使用一種技術來即時調整圖像大小,該技術使用.htaccess和一些php腳本。

在.htaccess中,我有這個:

# BEGIN ImageResizing
<ifmodule mod_rewrite.c>
 RewriteEngine on
 RewriteBase /

 RewriteRule ^img-small/([A-Za-z0-9/_-]+).(jpg|gif|png)$ images.php?max_width=100&imgfile=$1.$2
</ifmodule>
# END ImageResizing

images.php是執行實際大小調整的腳本

此正則表達式表達式:

^img-small/([A-Za-z0-9/_-]+).(jpg|gif|png)$

符合以下路徑:

http://yoursite.com/img-large/myimagepath/myimage.jpg

然后將其作為參數$ 1. $ 2發送到images.php腳本

images.php?max_width=100&imgfile=$1.$2

我的問題是:如果我有這樣的路徑,該如何工作:

http://yoursite.com/myimagepath/min_myimage.jpg

我試圖建立一個這樣的表達式:

^([A-Za-z0-9/_-]+/)+(min_)([A-Za-z0-9]+).(jpg|gif|png)$

Debuggex演示

我認為問題出在RewriteRule的第二部分

images.php?max_width=100&imgfile=$1.$2

我試圖更改為以下內容:

images.php?MAX_WIDTH = 100&imgfile = $ 1 + $ $ 3 4

這是PHP腳本的第一行:

// max_width and image variables are sent by htaccess

$max_height = 1000;

$image = $_GET["imgfile"];
$max_width = $_GET["max_width"];
if (strrchr($image, '/')) {
$filename = substr(strrchr($image, '/'), 1); // remove folder references
} else {
$filename = $image;
}

$size = getimagesize($image);
$width = $size[0];
$height = $size[1];

這對您有用嗎? 匹配的網址:http: http://yoursite.com/myimagepath/min_myimage.jpg

的.htaccess

RewriteEngine On
RewriteBase /
RewriteRule ^.*/([A-Za-z0-9/_-]+).(jpg|gif|png)$ images.php?max_width=100&imgfile=$1.$2

如果沒有,您得到哪個服務器錯誤?

我認為我們需要更確切地了解您要重寫處理的路徑和名稱的組合。 但這是適用於所有符合基本命名模式的圖像的模式,該命名模式以您指定的文件擴展名結尾

小提琴

暫無
暫無

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

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