2016-01-21 168 views
-1
<table border="1"> 
    <tr> 
    <th>id</th> 
    <th>filepath</th> 
    </tr> 
    <tr> 
    <th>1</th> 
    <th>uploads/1233/a.jpg</th> 
    </tr> 
    <tr> 
    <th>2</th> 
    <th>uploads/1213/b.jpg</th> 
    </tr> 
    <tr> 
    <th>3</th> 
    <th>uploads/1243/c.jpg</th> 
    </tr> 
    <tr> 
    <th>4</th> 
    <th>uploads/1234/d.jpg</th> 
    </tr> 
    <tr> 
    <th>5</th> 
    <th>uploads/1273/e.jpg</th> 
    </tr> 
</table> 

idfilepath來自mysql表。 我想要替換uploads文件夾和圖像名稱之間的數字值。從字符串中間替換文本

如何在mysql或php中執行此操作?

+0

請說明您的要求是否正確。 _我想在上傳文件夾之後和圖像文件_之前?有感覺嗎? –

+0

[這可能會幫助你](http://stackoverflow.com/questions/9261713/regex-strip-out-text-between-first-and-second-forward-slashes) –

+0

這是完整路徑上傳/ 1273/e .JPG ???你想刪除數值?糾正我...... – devpro

回答

1

嘗試這個例子:

<?php 
$str = "uploads/1234/d.jpg"; 
$strArray = explode("/", $str); 
$strOutput = $strArray[0]."/".$strArray[2]; 
echo $strOutput; 
?> 

在MySQL中,你可以嘗試這樣的事:

DELIMITER $$ 

CREATE FUNCTION customise_str(str_input varchar) RETURNS VARCHAR(100) 
    DETERMINISTIC 
BEGIN 
    DECLARE COUNT INT DEFAULT 9; 

    WHILE COUNT > 0 DO 
SET str_input = REPLACE(str_input, COUNT, ""); 
    SET COUNT = COUNT - 1; 
    END WHILE; 

RETURN (str_input); 
END $$ 
DELIMITER ;; 


update table_name set fielname = customise_str(fielname); 

我沒有嘗試過這個代碼。

,如果你不知道如何創建存儲功能,這些鏈接可能對你有所幫助:

http://www.mysqltutorial.org/mysql-stored-function/

https://dev.mysql.com/doc/refman/5.7/en/while.html

+0

感謝您的答案。是否有任何方法可以從mysql表字段中刪除所有數值,並且字段中值的類型是「上傳/ 1235/a.jpg「,」uploads/1270/b.jpg「等,但我想從字段值中刪除數值,如」uploads/a.jpg「,」uploads/b.jpg「等。 –

+0

我已經更新了我的答案。爲此,你需要在mysql中定義你自己的函數。 –

+0

如果我的答案適合您,請接受它。 –

1

嘗試這個功能的preg_replace PHP。

$filepath = "uploads/1233/a.jpg"; 
echo preg_replace('/(?<=\/)[1-9][0-9]*?(?=\/)/', "replacement_string_for_the_numeric_value", $filepath);