php 正则表达式 str_replace

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13528337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:37:23  来源:igfitidea点击:

Regex str_replace

phpmysqlregexurlstr-replace

提问by Mikkel Winther

Would it be possible to incorporate a str_replacemethod with a regular expression, designed to catch url strings in a simple html < textfield > input type?

是否有可能将一种str_replace带有正则表达式的方法结合起来,以在简单的 html < textfield > 输入类型中捕获 url 字符串?

I'm considering something simple like a tip for the user to set it up as follows:

我正在考虑一些简单的事情,比如让用户按如下方式设置它的提示:

This is some text and click this link here.

这是一些文字,请单击此处的链接。

Obviously the word "here" is a href to the url before it (or after it, if that makes a difference). The text input is drawn from a MySQL db.

显然,“此处”一词是指向其之前(或之后,如果有所不同)的 url 的 href。文本输入来自 MySQL 数据库。

I believe the start of my solution is something along the lines of:

我相信我的解决方案的开始是这样的:

$regex = '';
$pg = $row['pg'];
$pg = str_replace('{regex goes here}', $pg);

But I know things are missing. And then I would just output the $pg paragraph.

但我知道有些东西不见了。然后我只输出 $pg 段落。

回答by Wesley van Opdorp

Check out preg_replacehere, this is what you are lookin for.

看看preg_replace这里,这就是你要找的。

// From the documentation.
preg_replace($regularExpression, $replacement, $subject); 

回答by Asad Saeeduddin

What you're looking for is preg_replace:

你要找的是preg_replace

$pg = preg_replace('{regex goes here}', '{replacement goes here}', $pg);