Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP正则表达式替换未知字符串之前和之后_Php_Regex_String_Variables - Fatal编程技术网

PHP正则表达式替换未知字符串之前和之后

PHP正则表达式替换未知字符串之前和之后,php,regex,string,variables,Php,Regex,String,Variables,这简直快把我逼疯了 如果我有这样一个字符串,其中'udp://'后面的值会发生变化: LONDON Remote server cluster 88: taken udp://8379f690ba57e0fdf8f944324a4453de67780 BRUSSELS: Hosted MPLS cluster udp://5b0e9bc72b989c87a9a70da7865dbb5b4aa9c086 PARIS: Hosted atrium access udp://39415215319

这简直快把我逼疯了

如果我有这样一个字符串,其中'udp://'后面的值会发生变化:

LONDON Remote server cluster 88: taken
udp://8379f690ba57e0fdf8f944324a4453de67780

BRUSSELS: Hosted MPLS cluster
udp://5b0e9bc72b989c87a9a70da7865dbb5b4aa9c086

PARIS: Hosted atrium access
udp://39415215319c9brt6b9c7d10238c19cc82e7f88b

PRAGUE: Main Office acces
udp://e721f751ab0936706192cf15b4632889b38f131a
如何在PHP中使用正则表达式使其看起来像这样:

LONDON Remote server cluster 88: taken
http://www.8379f690ba57e0fdf8f944324a4453de67780/tsa.a3m

BRUSSELS: Hosted MPLS cluster
http://www.5b0e9bc72b989c87a9a70da7865dbb5b4aa9c086/tsa.a3m

PARIS: Hosted atrium access
http://www.39415215319c9brt6b9c7d10238c19cc82e7f88b/tsa.a3m

PRAGUE: Main Office acces
http://www.e721f751ab0936706192cf15b4632889b38f131a/tsa.a3m
到目前为止,我已经设法得到了这个正则表达式:

(?<=udp:\/\/)(.*)(?=)

(?不要使用lookback,因为它不会包含在匹配中,因此不会被替换

$data = preg_replace('#udp://(\S*)#', 'http://www.$1/tsa.a3m', $_POST['text']);

非常感谢,除了/tsa.a3m被添加到一个新行之外,它工作正常?我不知道为什么会发生这种情况。URL后面是否可能有类似

的HTML标记?我添加了一个演示,显示它可以正常使用普通字符串。Barmar,感谢演示,我认为问题与php如何s处理HTML表单中的输入文本。我在这里写了一个关于它的详细问题[我认为发生的事情是因为PHP从我的文本框中读取信息的方式,它包括换行符。你知道我可以用什么方式表达'#udp://(.*)#',但告诉它忽略后面立即出现的任何换行符-就好像(*)也在吸收换行符(我认为是^m),因此当/tsa.a3m添加到变量字符串的末尾时,它会出现在下一行。我已将regexp更改为使用
\S*
而不是
*
,因此它停止在任何空白处替换,包括任何类型的换行符。
$data = preg_replace('#udp://(\S*)#', 'http://www.$1/tsa.a3m', $_POST['text']);