简体   繁体   English

(PHP)如何查找以模式开头的单词并替换所有单词?

[英](PHP) How to find words beginning with a pattern and replace all of them?

I have a string. 我有一个字符串。 An example might be "Contact /u/someone on reddit, or visit /r/subreddit or /r/subreddit2" 例如,“在reddit上联系/ u / someone,或访问/ r / subreddit或/ r / subreddit2”

I want to replace any instance of "/r/x" and "/u/x" with " [/r/x](http://reddit.com/r/x) " and " [/u/x](http://reddit.com/u/x) " basically. 我想将“ / r / x”和“ / u / x”的任何实例替换为“ [/r/x](http://reddit.com/r/x) ”和“ [/u/x](http://reddit.com/u/x) ”。

So I'm not sure how to 1) find "/r/" and then expand that to the rest of the word (until there's a space), then 2) take that full "/r/x" and replace with my pattern, and most importantly 3) do this for all "/r/" and "/u/" matches in a single go... 所以我不确定如何1)找到“ / r /”,然后将其扩展到单词的其余部分(直到有空格),然后2)取完整的“ / r / x”并替换为我的模式,最重要的是3)一次性完成所有“ / r /”和“ / u /”匹配项的操作...

The only way I know to do this would be to write a function to walk the string, character by character, until I found "/", then look for "r" and "/" to follow; 我知道做到这一点的唯一方法是编写一个函数,逐个字符地移动字符串,直到找到“ /”,然后寻找“ r”和“ /”。 then keep going until I found a space. 然后继续前进,直到找到一个空间。 That would give me the beginning and ending characters, so I could do a string replacement; 那会给我开始和结束的字符,所以我可以做一个字符串替换。 then calculate the new end point, and continue walking the string. 然后计算新的终点,并继续遍历字符串。

This feels... dumb. 感觉...很蠢。 I have a feeling there's a relatively simple way to do this, and I just don't know how to google to get all the relevant parts. 我觉得有一个相对简单的方法可以做到这一点,而且我只是不知道如何通过Google获得所有相关部分。

A simple preg_replace will do what you want . 一个简单的preg_replace 完成您想要的工作

Try: 尝试:

$string = preg_replace('#(/(?:u|r)/[a-zA-Z0-9_-]+)#', '[\1](http://reddit.com\1)', $string);

Here is an example: http://ideone.com/dvz2zB 这是一个示例: http : //ideone.com/dvz2zB

You should see if you can discover what characters are valid in a Reddit name or in a Reddit username and modify the [a-zA-Z0-9_-] charset accordingly. 您应该查看是否可以发现Reddit名称或Reddit用户名中有效的字符,并相应地修改[a-zA-Z0-9_-]字符集。

In my opinion regex would be an overkill for such a simple operation. 在我看来,对于这样简单的操作,正则表达式可能会显得过大。 If you just want to replace instance of "/r/x" with "[r/x](http://reddit.com/r/x)" and "/u/x" with "[/u/x](http://reddit.com/u/x)" you should use str_replace although with preg_replace it'll lessen the code. 如果您只想用"[r/x](http://reddit.com/r/x)"替换“ / r / x”的实例,而用"[/u/x](http://reddit.com/u/x)" ”替换"[/u/x](http://reddit.com/u/x)" / u / x”的实例"[/u/x](http://reddit.com/u/x)" ,尽管使用preg_replace可以减少代码,但您应该使用str_replace

str_replace("/r/x","[/r/x](http://reddit.com/r/x)","whatever_string");

use regex for intricate search string and replace. 使用正则表达式查找复杂的搜索字符串并替换。 you can also use http://www.jslab.dk/tools.regex.php regular expression generator if you have something complex to capture in the string. 如果您需要在字符串中捕获一些复杂内容,也可以使用http://www.jslab.dk/tools.regex.php正则表达式生成器。

You are looking for a regular expression. 您正在寻找一个正则表达式。

A basic pattern starts out as a fixed string. 基本模式从固定字符串开始。 /u/ or /r/ which would match those exactly. /u//r/ ,它们将完全匹配。 This can be simplified to match one or another with /(?:u|r)/ which would match the same as those two patterns. 这可以简化为使用/(?:u|r)/来匹配一个或另一个,这将与这两个模式相同。 Next you would want to match everything from that point up to a space. 接下来,您要匹配从该点到空格的所有内容。 You would use a negative character group [^ ] which will match any character that is not a space, and apply a modifier, * , to match as many characters as possible that match that group. 您将使用负字符组[^ ]来匹配任何非空格字符,并应用修饰符* ,以匹配与该组尽可能多的字符。 /(?:u|r)/[^ ]*

You can take that pattern further and add a lookbehind, (?<= ) to ensure your match is preceded by a space so you're not matching a partial which results in (?<= )/(?:u|r)/[^ ]* . 您可以进一步采用该模式,并在(?<= )添加一个后缀,以确保您的匹配项前面有一个空格,这样就不会匹配导致(?<= )/(?:u|r)/[^ ]* You wrap all of that to make a capturing group ((?<= )/(?:u|r)/[^ ]*) . 您将所有这些包装起来以组成捕获组((?<= )/(?:u|r)/[^ ]*) This will capture the contents within the parenthesis to allow for a replacement pattern. 这将捕获括号内的内容,以允许使用替换模式。 You can express your chosen replacement using the \\1 reference to the first captured group as [\\1](http://reddit.com\\1) . 您可以使用对第一个捕获组的\\1引用表示为[\\1](http://reddit.com\\1)来表示您选择的替换项。

In php you would pass the matching pattern, replacement pattern, and subject string to the preg_replace function. 在php中,您可以将匹配模式,替换模式和主题字符串传递给preg_replace函数。

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

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