preg-replace

Converting preg_replace to preg_replace_callback for finding and replacing words with variables

不问归期 提交于 2020-04-07 09:14:12
问题 I have the following line of code: $message = preg_replace('/\{\{([a-zA-Z_-]+)\}\}/e', "$$1", $body); This replaces words surrounded by two curly brackets with variables of the same name. ie {{username}} gets replaced by $username. I am trying to convert it to use preg_replace_callback. This is my code so far based on Googling, but I'm not really sure what I am doing! The error_log output is showing the variable name including the curly brackets. $message = preg_replace_callback( "/\{\{([a-zA

Converting preg_replace to preg_replace_callback for finding and replacing words with variables

[亡魂溺海] 提交于 2020-04-07 09:14:10
问题 I have the following line of code: $message = preg_replace('/\{\{([a-zA-Z_-]+)\}\}/e', "$$1", $body); This replaces words surrounded by two curly brackets with variables of the same name. ie {{username}} gets replaced by $username. I am trying to convert it to use preg_replace_callback. This is my code so far based on Googling, but I'm not really sure what I am doing! The error_log output is showing the variable name including the curly brackets. $message = preg_replace_callback( "/\{\{([a-zA

Extract first integer in a string with PHP

我只是一个虾纸丫 提交于 2020-03-10 04:26:49
问题 Consider the following strings: $strings = array( "8.-10. stage", "8. stage" ); I would like to extract the first integer of each string, so it would return 8 8 I tried to filter out numbers with preg_replace but it returns all integers and I only want the first. foreach($strings as $string) { echo preg_replace("/[^0-9]/", '',$string); } Any suggestions? 回答1: A convenient (although not record-breaking in performance) solution using regular expressions would be: $string = "3rd time's a charm";

How to remove <div> tags from post contents

拈花ヽ惹草 提交于 2020-03-06 04:32:30
问题 I have migrated my contents from a Yii site to Wordpress and I have all my posts filled with div tags with all kind of styles and ids and class and I want to use a pattern to remove them all. I searched the internet and came with this function, yet it does remove the closing tag, but not the opening tags which contains other attributes like style id or class: function remove_divs($data) { $filteredContent = preg_replace(array('/<\s*div.*?>/', '</div>'), '', $data['post_content']); $data['post

How to remove <div> tags from post contents

寵の児 提交于 2020-03-06 04:30:00
问题 I have migrated my contents from a Yii site to Wordpress and I have all my posts filled with div tags with all kind of styles and ids and class and I want to use a pattern to remove them all. I searched the internet and came with this function, yet it does remove the closing tag, but not the opening tags which contains other attributes like style id or class: function remove_divs($data) { $filteredContent = preg_replace(array('/<\s*div.*?>/', '</div>'), '', $data['post_content']); $data['post

regex to remove bbcode tags with atrributes

限于喜欢 提交于 2020-02-07 06:48:29
问题 I've got a number of bbcode tags that have phpbb attributes (5 digit value - assuming text color or something). They look like this in the text: This is [b:31747]bold[/b:31747] text and so is [b:17171]this[/b:17171]. I cannot get a regex working that finds bracket+b+colon+any_combo_of_5_digits+end_bracket and lets me replace it with corresponding html. Using php's preg_replace() function, if it makes a difference. 回答1: The regular expression you need is: \[/?b:\d{5}] 回答2: This would replace

regex to remove bbcode tags with atrributes

旧时模样 提交于 2020-02-07 06:48:12
问题 I've got a number of bbcode tags that have phpbb attributes (5 digit value - assuming text color or something). They look like this in the text: This is [b:31747]bold[/b:31747] text and so is [b:17171]this[/b:17171]. I cannot get a regex working that finds bracket+b+colon+any_combo_of_5_digits+end_bracket and lets me replace it with corresponding html. Using php's preg_replace() function, if it makes a difference. 回答1: The regular expression you need is: \[/?b:\d{5}] 回答2: This would replace

regex to remove bbcode tags with atrributes

旧城冷巷雨未停 提交于 2020-02-07 06:48:04
问题 I've got a number of bbcode tags that have phpbb attributes (5 digit value - assuming text color or something). They look like this in the text: This is [b:31747]bold[/b:31747] text and so is [b:17171]this[/b:17171]. I cannot get a regex working that finds bracket+b+colon+any_combo_of_5_digits+end_bracket and lets me replace it with corresponding html. Using php's preg_replace() function, if it makes a difference. 回答1: The regular expression you need is: \[/?b:\d{5}] 回答2: This would replace

Add +1 to a string obtained from another site

僤鯓⒐⒋嵵緔 提交于 2020-02-07 05:39:09
问题 I have a string I get from a website. A portion of the string is "X2" I want to add +1 to 2. The entire string I get is: 20120815_00_X2 What I want is to add the "X2" +1 until "20120815_00_X13" 回答1: You can do : $string = '20120815_00_X2'; $concat = substr($string, 0, -1); $num = (integer) substr($string, -1); $incremented = $concat . ($num + 1); echo $incremented; For more informations about substr() see => documentation 回答2: You want to find the number at the end of your string and capture

How to remove link with preg_replace();?

折月煮酒 提交于 2020-02-04 07:00:29
问题 I'm not sure how to explain this, so I'll show it on my code. <a href="link.php">First</a> and <a href="link.php" class="delete">Second</a> and <a href="link.php">Third</a> how can I delete opening <a href="link.php" class="delete"> and closing </a> but not the rest? I'm asking for preg_replace(); and I'm not looking for DomDocument or others methods to do it. I just want to see example on preg_replace(); how is it achievable? 回答1: Only pick the groups you want to preserve: $pattern = '~(<a