preg-replace

How to loop through, match and replace?

丶灬走出姿态 提交于 2020-01-11 11:25:12
问题 I have multiple strings with same curly braces I want to replace them as dynamic if I get the count as 1 then need to replace the first occurrence, If count as 2 then replaces the second occurrence as so on until condition satisfies. <?php include_once("con.php"); $db = new Da(); $con = $db->con(); $String = "{{ONE}} {{TWO}} {{THREE}} {{FOUR}} {{FIVE}} {{SIX}}"; $Count = 1; if(preg_match_all("/\{\{[^{}]+\}\}/", $lclString, $matches)) { foreach ($matches[0] as $match) { $Count++; $Query =

This regex is cutting off the last word in the string even though strlen is within acceptable range

ぃ、小莉子 提交于 2020-01-11 11:08:46
问题 $theExcerpt = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis' $theExcerptAppend = (strlen($theExcerpt) > 156) ? '...' : ''; $theExcerpt = preg_replace('/\s+?(\S+)?$/', '', substr($theExcerpt, 0, 156)); $theExcerpt .= $theExcerptAppend; As long as the input phrase length exceeds 156 characters, the script works fine. However, when the length is less than 156 (as it is here at 154), the

preg_replace how surround html attributes for a string with " in PHP

廉价感情. 提交于 2020-01-11 10:26:15
问题 I have a string variable in PHP , its content is: $var='<SPAN id=1 value=1 name=1> one</SPAN> <div id=2 value=2 name=2> two</div >'; .... I need a function for surround html attributes with "" i need do this for the all meta-tag ,etc the result should be this: $var='<SPAN id= "1" value="1" name="1"> one </SPAN> <div id="2" value="2" name="2" > two</div >'; ... I need replace all =[a-z][A-Z][1-9] for ="[a-z][A-Z][1-9]". I need a regular expresion for preg_replace 回答1: You need to wrap it all

regex to replace a given word with space at either side or not at all

谁说我不能喝 提交于 2020-01-11 10:23:27
问题 I am working with some code in PHP that grabs the referrer data from a search engine, giving me the query that the user entered. I would then like to remove certain stop words from that string if they exist. However, the word may or may not have a space at either end. For example, I have been using str_replace to remove a word as follows: $keywords = str_replace("for", "", $keywords); $keywords = str_replace("sale", "", $keywords); but if the $keywords value is "baby formula" it will change

regex to replace a given word with space at either side or not at all

ぃ、小莉子 提交于 2020-01-11 10:22:11
问题 I am working with some code in PHP that grabs the referrer data from a search engine, giving me the query that the user entered. I would then like to remove certain stop words from that string if they exist. However, the word may or may not have a space at either end. For example, I have been using str_replace to remove a word as follows: $keywords = str_replace("for", "", $keywords); $keywords = str_replace("sale", "", $keywords); but if the $keywords value is "baby formula" it will change

Matching end of line position using m flag with different line ending styles

霸气de小男生 提交于 2020-01-11 10:17:27
问题 I'm trying to wrap each line that starts with "## " with tags. Trying to achieve a GitHub/Stackoverflow-like syntax for text formatting. This is what I got: $value = preg_replace('/^## (.*)$/m', '<p>$1</p>', $value); After googling for quite a while this seems the right solution, however it doesn't work as expected or I just don't understand something. Example text: ## Some header 1 Some text that doesn't need to be altered ## Some header 2 And this is the result: <p>Some header 1 </p> Some

preg_replace to exclude <a href='''></a> PHP

空扰寡人 提交于 2020-01-11 09:46:10
问题 Im using preg_replace to replace keywords in text with a href tag, my regex is working awesome, right now my code is: $newstring2 = preg_replace("/\p{L}*?".preg_quote($match[$i])."\p{L}*/ui", "<a href='".$url."' class='link'>$0</a>", $newstring); Only problem with this is, that I need to exclude any keywords inside <a href='https://keyword.cz' title="keyword">keyword</a> This is what I found https://stackoverflow.com/a/22821650/4928816 So is here someone who can help me merge this two regex

Avoid replace placeholder found in replacement string

回眸只為那壹抹淺笑 提交于 2020-01-11 06:46:48
问题 let's say i have this string: "my string ? other string ?" I want to replace the first "?" with "first param ?" (note the placeholder ? inside the text) and the second "second param". IF i do a preg_replace i obtain this: my string first param second param other string ? ^^^^^^^^^^^^^^^^^^^^^^^^ ^ WRONG NOT REPLACED Basically since the first replacement has also the placeholder, preg_replace is stupid enough to replace that placeholder, instead of the real second one at the end. Code with

Replace the last comma in a string using Regular Expression

烂漫一生 提交于 2020-01-09 23:18:11
问题 I have a string like: "item 1, item 2, item 3" . What I need is to transform it to: "item 1, item 2 and item 3" . In fact, replace the last comma with " and". Can anyone help me with this? 回答1: Use greediness to achieve this: $text = preg_replace('/(.*),/','$1 and',$text) This matches everything to the last comma and replaces it through itself w/o the comma. 回答2: This regex finds last coma: (,)[^,]*$ 来源: https://stackoverflow.com/questions/6705509/replace-the-last-comma-in-a-string-using

replace ereg_replace with preg_replace [duplicate]

…衆ロ難τιáo~ 提交于 2020-01-09 04:56:46
问题 This question already has answers here : How can I convert ereg expressions to preg in PHP? (4 answers) Closed 7 months ago . Hi need to change the function ereg_replace("[\]", "", $theData) to preg_replace 回答1: To port ereg_replace to preg_replace you need to put the regex between a pair of delimiter Also your regx is [\] is invalid to be used for preg_replace as the \ is escaping the closing char class ] The correct port is preg_replace('/[\\\]/','',$theData) Also since the char class has