preg-replace

Php preg_replace with array in elements

跟風遠走 提交于 2021-02-05 11:03:23
问题 I am trying to do the emoji system and if the element in the array passes through the page, turn the value on the opposite side. Example: $emoticons = array( ':)' => '<img src="assets/smiles/smilesblank.png" alt="smile" class="img-responsive" />', ':-)' => '<img src="assets/smiles/smilesblank.png" alt="smile" class="icon_smile" />', ':D' => '<img src="assets/smiles/smilesblank.png" alt="smile" class="icon_laugh" />', ':d' => '<img src="assets/smiles/smilesblank.png" alt="laugh" class="icon

How to replace text with a regex pattern and integrate a counter in the replacement text?

牧云@^-^@ 提交于 2021-02-05 10:00:18
问题 function parse($string){ $counter = 0; $string = preg_replace("_\[b\](.*?)\[/b\]_si", '<span class="b">'. $counter .'. $1</span>', $string, -1, $counter); return $string; } I'm trying to make a ubb parser, that parses tags and put the counting in front of it: [b]Hey[/b] [b]Hello[/b] Should return this: <span class="b">1. Hey</span> <span class="b">2. Hello</span> But is returning this: <span class="b">1. Hey</span> <span class="b">1. Hello</span> So beside the function above, I've tried this:

how to use regex special characters in pattern in preg_replace

隐身守侯 提交于 2021-02-05 08:11:37
问题 I am trying to replace 2.0 to stack, but the following code replace 2008 to 2.08 Following is my code: $string = 'The story is inspired by the Operation Batla House that took place in 2008 '; $tag = '2.0'; $pattern = '/(\s|^)'.($tag).'(?=[^a-z^A-Z])/i'; echo preg_replace($pattern, '2.0', $string); 回答1: Use preg_quote and make sure you pass the regex delimiter as the second argument: $string = 'The story is inspired by the Operation Batla House that took place in 2008 '; $tag = '2.0'; $pattern

how to use regex special characters in pattern in preg_replace

若如初见. 提交于 2021-02-05 08:09:47
问题 I am trying to replace 2.0 to stack, but the following code replace 2008 to 2.08 Following is my code: $string = 'The story is inspired by the Operation Batla House that took place in 2008 '; $tag = '2.0'; $pattern = '/(\s|^)'.($tag).'(?=[^a-z^A-Z])/i'; echo preg_replace($pattern, '2.0', $string); 回答1: Use preg_quote and make sure you pass the regex delimiter as the second argument: $string = 'The story is inspired by the Operation Batla House that took place in 2008 '; $tag = '2.0'; $pattern

Regex pattern matching literal repeated \n

爱⌒轻易说出口 提交于 2021-02-05 07:11:25
问题 Given a literal string such as: Hello\n\n\n\n\n\n\n\n\n\n\n\nWorld I would like to reduce the repeated \n 's to a single \n . I'm using PHP, and been playing around with a bunch of different regex patterns. So here's a simple example of the code: $testRegex = '/(\\n){2,}/'; $test = 'Hello\n\n\n\n\n\n\n\n\nWorld'; $test2 = preg_replace($testRegex ,'\n',$test); echo "<hr/>test regex<hr/>".$test2; I'm new to PHP, not that new to regex, but it seems '\n' conforms to special rules. I'm still

Regex: How not to replace specific word in any html tag?

旧城冷巷雨未停 提交于 2021-02-04 19:20:45
问题 So let's assume I have a text like this: This is a great test! We're testing something awesome. Click here to <a href="whatever">test it!</a>. I want to add some color to the word "test", but not if it's in an a tag. I've tried doing this: /(?<!href="(.*?)">)test/ But it doesn't work. It works like this: /(?<!href="whatever">)test/ But of course I have many links, so this isn't an option. The whole code would be something like this: $replacement = preg_replace('/(?<!href="SOLUTION HERE">)test

Regex: How not to replace specific word in any html tag?

大兔子大兔子 提交于 2021-02-04 19:19:05
问题 So let's assume I have a text like this: This is a great test! We're testing something awesome. Click here to <a href="whatever">test it!</a>. I want to add some color to the word "test", but not if it's in an a tag. I've tried doing this: /(?<!href="(.*?)">)test/ But it doesn't work. It works like this: /(?<!href="whatever">)test/ But of course I have many links, so this isn't an option. The whole code would be something like this: $replacement = preg_replace('/(?<!href="SOLUTION HERE">)test

PHP preg_split get rid of trailing spaces in just one line?

こ雲淡風輕ζ 提交于 2021-01-29 03:09:29
问题 How can i get rid of trailing spaces in preg_split result without using preg_replace to first remove all spaces from $test string? $test = 'One , Two, Thee '; $test = preg_replace('/\s+/', ' ', $test); $pieces = preg_split("/[,]/", $test); 回答1: If it must be preg_split() (you actually required that in the question) then this might help: $test = 'One , Two, Thee '; $pieces = preg_split("/\s*,\s*/", trim($test), -1, PREG_SPLIT_NO_EMPTY); trim() is used to remove space before the first and

Dynamically replace part of a condition block with regex

有些话、适合烂在心里 提交于 2020-12-12 10:50:10
问题 There is a string in format: else if($rule=='somerule1') echo '{"s":1,"n":"name surname"}'; else if($rule=='somerule2') echo '{"s":1,"n":"another text here"}'; ... "s" can have only number, "n" any text. In input I have $rule value, and I need to remove the else if block that corresponds to this value. I am trying this: $str = preg_replace("/else if\(\$rule=='$rule'\)\necho '{\"s\":[0-9],\"n\":\".*\"/", "", $str); where $str is a string, that contains blocks I mentioned above, $rule is a