preg-replace

using php preg_replace to replace a whole character string and not replace if string is part of a longer string

假装没事ソ 提交于 2020-06-27 01:38:17
问题 Regular Expressions are completely new to me and having done much searching my expression for testing purposes is this: preg_replace('/\b0.00%\b/','- ', '0.00%') It yields 0.00% when what I want is - . With preg_replace('/\b0.00%\b/','- ', '50.00%') yields 50.00% which is what I want - so this is fine. But clearly the expression is not working as it is not, in the first example replacing 0.00% with - . I can think of workarounds with if(){} for testing length/content of string but presume the

How to remove all non-uppercase characters in a string?

北战南征 提交于 2020-06-02 06:30:56
问题 Yeah I'm basically just trying to explode a phrase like Social Inc. or David Jason to SI and DJ . I've tried using explode but couldn't figure out how to explode everything BUT the capital letters, do I need to use preg_match() ? 回答1: You can use this regex (?![A-Z]). with preg_replace() to replace every char except the one in uppercase. preg_replace("/(?![A-Z])./", "", $yourvariable) The regex will look for anythings NOT an uppercase letter ( ?! negative lookahead ). I've created a regex101

php preg_replace not doing anything

空扰寡人 提交于 2020-05-09 17:22:30
问题 For some reason my preg_replace call is not working, I have check everything I can think of to no avail. Any suggestions? foreach ($this->vars as $key=>$var) { preg_replace("/\{$key\}/", $var, $this->tempContentXML); } vars is an array containing the $key->value that needs to be replaced in the string, tempContentXML is a string containing XML data. Piece of the string ...<table:table-cell table:style-name="Table3.B1" office:value-type="string"><text:p text:style-name="P9">{Reference}</text:p

php preg_replace not doing anything

試著忘記壹切 提交于 2020-05-09 17:21:11
问题 For some reason my preg_replace call is not working, I have check everything I can think of to no avail. Any suggestions? foreach ($this->vars as $key=>$var) { preg_replace("/\{$key\}/", $var, $this->tempContentXML); } vars is an array containing the $key->value that needs to be replaced in the string, tempContentXML is a string containing XML data. Piece of the string ...<table:table-cell table:style-name="Table3.B1" office:value-type="string"><text:p text:style-name="P9">{Reference}</text:p

how to define regex (preg_replace) to removes space between number character

人盡茶涼 提交于 2020-05-09 10:53:17
问题 I have string like this: $str = "old iccid : 809831 3245 345 new iccid : 999000 112221" How to define regex in order to remove the space character between number character in PHP, to become this output? $output = "old iccid : 8098313245345 new iccid : 999000112221"; i've tried to use this syntax: $output = preg_replace( '/\d[ *]\d/', '', $str); but didn't work well. 回答1: Try: $output = preg_replace('/(?<=\d)\s+(?=\d)/', '', $str); 回答2: The regex engine can traverse the string faster / with

how to define regex (preg_replace) to removes space between number character

你。 提交于 2020-05-09 10:51:28
问题 I have string like this: $str = "old iccid : 809831 3245 345 new iccid : 999000 112221" How to define regex in order to remove the space character between number character in PHP, to become this output? $output = "old iccid : 8098313245345 new iccid : 999000112221"; i've tried to use this syntax: $output = preg_replace( '/\d[ *]\d/', '', $str); but didn't work well. 回答1: Try: $output = preg_replace('/(?<=\d)\s+(?=\d)/', '', $str); 回答2: The regex engine can traverse the string faster / with

Remove all non-numeric characters from a string in PHP

拈花ヽ惹草 提交于 2020-04-18 08:38:14
问题 I'm trying to remove all non-numeric characters from a string. This is the string: $price = '₪1,180.00'; (the first character is the new israeli shekel currency symbol) I tried to use: $price_numeric_value = preg_replace( '/\D/', '', $price ); echo '<pre>';var_dump( $price_numeric_value );echo '</pre>'; $price_numeric_value = preg_replace( '~\D~', '', $price ); echo '<pre>';var_dump( $price_numeric_value );echo '</pre>'; $price_numeric_value = preg_replace( '/[^0-9.]/', '', $price ); echo '

Converting preg_replace to preg_replace_callback for finding and replacing words with variables

一笑奈何 提交于 2020-04-07 09:15:31
问题 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:42
问题 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