使用PHP進行正則表達式字符串替換和拆分

[英]Regex String Replacement and Split using PHP


I have a sample string in PHP:

我在PHP中有一個示例字符串:

$var = 'lurm limsrh domw en wonf src="@storage//user1/path/file.txt" and any lorm lipsm tjp quor it src="@storage//user2/path/file.txt" utt fine @storage// no chnge';

I want following operation using PHP:

我想使用PHP進行以下操作:

  1. Replace only those @storage// which is inside src="" to http://mydomain/folder/
  2. 只將那些位於src =“”內的@vore //替換為http:// mydomain / folder /
  3. Replace all user to md5(user);. The username must comes after @storage// i.e. it comes between '@storage//' and the next '/'. Also replace only those which are inside src=""
  4. 將所有用戶替換為md5(用戶);.用戶名必須在@ storage //之后,即它位於'@ storage //'和下一個'/'之間。也只替換src =“”里面的那些

Which regular expression can I use for that?

我可以使用哪種正則表達式?

1 个解决方案

#1


1  

$url = 'http://mydomain/folder/';
$var = preg_replace_callback('/(.*?\bsrc)\s*=\s*"\s*@storage\/\/([^\/]+)([^"]+?)\s*"/s', function($matches) use ($url) {
    return $matches[1] . '="' . $url . md5($matches[2]) . $matches[3] . '"';
}, $var);

Sorry, can't test, but should be right.

對不起,無法測試,但應該是對的。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/07/30/7255c3fcb2c5d1fa943da63ca574d81e.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com