简体   繁体   English

的php:preg_match和preg_replace

[英]php: preg_match and preg_replace

I'm unsure of how to do the following... 我不确定如何执行以下操作...

I need to search through a string and match all instances of a forward slash and certain letters . 我需要搜索一个string并匹配所有forward slashcertain letters实例。 This is for word modifications that users have the ability to input and I want them to be able to modify individual words. 这是为了用户可以输入的单词修改,我希望他们能够修改单个单词。

Here is an example string 这是一个示例字符串

Hello, isn't the weather just absolutely beautiful today!? 你好,今天的天气不是绝对美好吗?

What I'd like the user to be able to do is something like this 我希望用户能够执行以下操作

Hello, isn't the /bo weather just /it beautiful today!? 您好,今天/ bo的天气不是很美吗?

take note of the /bo and /it 记下/bo/it

what I'd like to do is have a preg_match and or preg_replace statement that finds and replaces the instances of /bo and /it and converts them instead into html tags such as bolded html tag and italics html tag (i cant type them here or they get converted into actual html. but wrap themselves around the word immediately following the /bo so in this example it would wind up being 我想做的是有一个preg_matchpreg_replace语句,该语句查找并替换/bo/it的实例,并将其转换为html标签,例如bolded html tagitalics html tag (我不能在此处键入它们,或者他们会转换成实际的html。,但会在/bo后面紧紧环绕单词,因此在此示例中它最终会被

Hello, isn't the <b>weather</b> just <i>beautiful</i> today!?

Any ideas how I could do this with a regex ? 有什么想法我可以用regex来做到这一点吗?

Once the conversions are done i'll do the standard sanitizing before inserting the data into the database along with prepared statements. 转换完成后,在将数据与准备好的语句一起插入数据库之前,我将进行标准清理。

$string = "Hello, isn't the /bo weather just /it beautiful /bo today!?";

var_dump(preg_replace (array('/\/bo\s(\w+)/', '/\/it\s(\w+)/'), array('<b>$1</b>', '<i>$1</i>'), $string));

"Hello, isn't the weather just beautiful today !?" “你好, 今天天气不是很美吗??”

You can use preg_replace_callback for this purpose. 您可以为此目的使用preg_replace_callback
This basically calls a callback method for every match that occurs. 对于发生的每个匹配,这基本上都会调用一个回调方法。
Inside the callback, you can perform the replacement according to your conditions 在回调中,您可以根据情况执行替换
(bold for bo, italics for it, heading for he et al.) (bo的粗体,斜体的斜体,他等人的标题。)

Something like this - 像这样-

$str = "Hello, isn't the /bo weather just /it beautiful today!?";
$regex = "/\/(.*?)\s+(.+?)\b/";

function callback($matches){
    $type = $matches[1];
    $text = $matches[2];
    switch($type){
        case "bo":
            return "<b>".$text."</b>";
            break;
        case "it":
            return "<i>".$text."</i>";
            break;
        default:
            return $text;
    }   
}

$resp = preg_replace_callback(
    $regex,
    "callback",
    $str
);

var_dump($resp);

/*
OUTPUT- 
Hello, isn't the <b>weather</b> just <i>beautiful</i> today!?
*/

This example could be extended further by checking for various types and invalid types 通过检查各种类型和无效类型,可以进一步扩展此示例

The regexp 正则表达式

/\/(bo|it)\s+([\S]+)(?=\b)/g

and replacement string 和替换字符串

<$1>$2</$1>

would almost do it: 几乎可以做到:

Hello, isn't the <bo>weather</bo> just <it>beautiful</it> today!?

But the tags are not quite right yet ... They need to be single letters. 但是标签还不太正确...它们必须是单个字母。 :-( :-(

Try here: https://regex101.com/r/oB9gT0/1 在这里尝试: https : //regex101.com/r/oB9gT0/1

2. Edit - a bit late, but now it works: 2.编辑-有点晚了,但是现在可以了:

$str=preg_replace('/\/([bi])((?<=b)o|(?<=i)t)\s+([\w]+)/','<$1>$3</$1>',$str);

will now deliver the correct result: 现在将提供正确的结果:

Hello, isn't the <b>weather</b> just <i>beautiful</i> today!?

see here: https://regex101.com/r/oB9gT0/3 看到这里: https : //regex101.com/r/oB9gT0/3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM