preg-replace

Replacing specified double quotes in text with preg_replace

岁酱吖の 提交于 2019-12-31 04:04:12
问题 I have got a serialized array, and I need to replace double quotes in all places like this: ...s:30:"test "is" & test";... to ...s:30:"test "is" & test";... There can be a lot of quotes in the text, so can somebody help with it? 回答1: Try preg_replace("/([^:])(\")([^;:])+/isU","$1"$3",$arr); 来源: https://stackoverflow.com/questions/12037289/replacing-specified-double-quotes-in-text-with-preg-replace

Remove BBCode tags and their content in PHP [duplicate]

穿精又带淫゛_ 提交于 2019-12-31 01:25:20
问题 This question already has answers here : Closed 8 years ago . Possible Duplicates: Recursive BBCode Parsing Strip BBCode via RegEx What is the best way to remove all BBCode tags of a string and their content in PHP? 回答1: <?php function stripBBCode($text_to_search) { $pattern = '|[[\/\!]*?[^\[\]]*?]|si'; $replace = ''; return preg_replace($pattern, $replace, $text_to_search); } echo stripBBCode($text_to_search); ?> demo 来源: https://stackoverflow.com/questions/6777257/remove-bbcode-tags-and

Multiple replace (probably preg_replace) of same string with array

六月ゝ 毕业季﹏ 提交于 2019-12-30 11:55:07
问题 I need to replace multiple instances of a certain string (question mark) with strings from an array. e.g. if the string I wish to replace appears 3 times and my array has a length of 3, the first one would be replaced by the first item in the array, the second by the second etc etc. You may recongise it's quite similar to the way prepared statements work in mysqli. Here's an example: $myArray = array( [0] => 'yellow', [1] => 'green', [2] => 'red' ); $myString = 'banana is ?, apple is ?,

Multiple replace (probably preg_replace) of same string with array

折月煮酒 提交于 2019-12-30 11:55:06
问题 I need to replace multiple instances of a certain string (question mark) with strings from an array. e.g. if the string I wish to replace appears 3 times and my array has a length of 3, the first one would be replaced by the first item in the array, the second by the second etc etc. You may recongise it's quite similar to the way prepared statements work in mysqli. Here's an example: $myArray = array( [0] => 'yellow', [1] => 'green', [2] => 'red' ); $myString = 'banana is ?, apple is ?,

What would be a regex to replace/remove END where its not been preceded by an unended START?

折月煮酒 提交于 2019-12-30 10:27:18
问题 What would be a regex (PHP) to replace/remove (using preg_replace() ) END where its not been preceded by an unended START? Here are a few examples to portray what I mean better: Example 1: Input: sometext....END Output: sometext.... //because theres no START, therefore no need for the excess END Example 2: Input: STARTsometext....END Output: STARTsometext....END //because its preceded by a START Example 3: Input: STARTsometext....END.......END Output: STARTsometext....END....... //because the

How to remove redundant <br /> tags from HTML code using PHP?

大城市里の小女人 提交于 2019-12-30 09:59:30
问题 I'm parsing some messy HTML code with PHP in which there are some redundant tags and I would like to clean them up a bit. For instance: <br> <br /><br /> <br> How would I replace something like that with this using preg_replace()?: <br /><br /> Newlines, spaces, and the differences between <br> , <br/> , and <br /> would all have to be accounted for. Edit: Basically I'd like to replace every instance of three or more successive breaks with just two. 回答1: Here is something you can use. The

Escape Regex Newline

吃可爱长大的小学妹 提交于 2019-12-30 08:26:10
问题 How would I make a \n match in regex? I want the actual two ASCII values 92 and 110 to be matched (as a string). I'm using preg from PHP Thanks 回答1: You can either escape the first slash: \\n Or wrap the first slash in []: [\]n 回答2: if you don't want to match a real linebreak but a string (with two characters) like '\n' then you just have to escape the backslash with another one \\n so that it will not be recognized as linebreak. But most programming languages are a bit different when it

Replace all occurrences inside pattern

好久不见. 提交于 2019-12-30 08:23:52
问题 I have a string that is like this {{ some text @ other text @ and some other text }} @ this should not be replaced {{ but this should: @ }} I want it to become {{ some text ### other text ### and some other text }} @ this should not be replaced {{ but this should: ### }} I guess the example is straight forward enough and I'm not sure I can better explain what I want to to achieve in words. I tried several different approaches but none worked. 回答1: This can be achieved with a regular

What is the best way to remove punctuation marks, symbols, diacritics, special characters?

雨燕双飞 提交于 2019-12-30 07:59:09
问题 I use these lines of code to remove all punctuation marks, symbols, etc as you can see them in the array, $pattern_page = array("+",",",".","-","'","\"","&","!","?",":",";","#","~","=","/","$","£","^","(",")","_","<",">"); $pg_url = str_replace($pattern_page, ' ', strtolower($pg_url)); but I want to make it simpler as it looks silly to list all the stuff I want to remove in the array as there might be some other special characters I want to remove. I thought of using the regular expression

Prevent double space when entering username

半世苍凉 提交于 2019-12-30 07:33:50
问题 When users register to my website I want to allow them to use spaces in their username, but only one space per word. My current code: $usor = $_POST['usernameone']; $allowed = "/[^a-z0-9 ]/i"; $username = preg_replace($allowed,"",$usor); $firstlettercheck = $username[0]; $lastlettercheck = substr("$username", -1); if ($firstlettercheck == " " or $lastlettercheck == " ") { echo "Usernames can not contain a space at start/end of username."; } What do I need to add to ensure there is only one