This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to replace text over multiple lines using preg_replace
I am trying to read some text from a file and than replacing some pattern. If I try to replace the patern from just a single string it works, but if there are multiple such strings in a file it doesnt work.
$this->session->set_flashdata('error_message', 'Naslovna vrstica je bila uspešno shranjena');
This is an example of text that I am trying to replace the replacment works ok with just this line, but not if there are other such lines in a file, which all match individualy though.
$content = file_get_contents("C:\Users\Borut\\test.txt");
$pattern="/^.*session->set_flashdata\((.*),(.*)\);$/";
$replacement="\$_SESSION[$1]=$2";
this is my code. How do you replace multiple strings as the one shown above.
The modifier you want is m. You can find all modifiers here
That said, the easiest and better regex solution would be
"/\$this->session->set_flashdata\((.*?),\s*(.*?)\);/"
Notice how there's a ? after the .* in each. This is to stop greedy matching as with yours. Also notice that the modifier isn't required either with the removal of the ^ and $
Related
This question already has answers here:
What does the $1$2$4 mean in this preg_replace?
(3 answers)
Closed 4 years ago.
I want to loop through an array converting specific key/value pairs that contain markup to HTML.
So an example value for $comment['comment_text'] would be:
This has *bolded* text
And should become:
This has <strong>bolded</strong> text
Here's what I've tried:
$pattern = "/\*\b.*?\b\*/i";
$newComment = preg_replace($pattern, "<strong>$&</strong>",
$comment['comment_text']);
And what I get:
This has $& text
I realize I'm mashing up Javascript with PHP, but reading about back references in PHP hasn't made things any clearer.
My strings may have multiple bolded (in markup) instances...
Any help appreciated.
UPDATE:
Apologies - I didn't realize that Stackoverflow was converting asterisks to italics. I converted the example to code.
Also, my confusion came down to the use of $0 vs. $1. Which I still don't fully understand. I thought the numbers referred to the matches in the string...so if you had 5 instances you could refer to them by $0 through $4.
If you use $0 you get:
This has <strong>*bolded*</strong> text
But if you use $1 you get the desired result.
Do this.
$pattern = "/\*\b(.*?)\b\*/";
$newComment = preg_replace($pattern, "<strong>$1</strong>", $comment['comment_text']);
Here $1 refers to the group 1 match. Here I'm supposing that you want to make text between ** bolded.
This question already has answers here:
PHP - BBCode parser - recursive [quote] with regex and preg_replace
(2 answers)
Closed 7 years ago.
Here is my text:
$msg_text = '[quote]TEXT[/quote]';
and my preg_replace:
$msg_text = preg_replace('#\[quote\](.*?)\[\/quote\]#is', '"$1"'."\r\n", $msg_text);
And it works fine. But what when my text is looking like that:
$msg_text = '[quote]TEXT [quote]TEXT[/quote][/quote]';
?? My preg_replace doesn't work on this example. How I can replcace this text in all instances ?
Just remove the ? after .* which will remove the laziness of your pattern.
'#\[quote\](.*)\[\/quote\]#is'
What is Laziness in Regex?
Patterns like .* and .+ normally greedy, which means they will try to match as long as they can. Putting an extra ? after it we tell it to become lazy, so it will take only nearest / shortest match. So, if you remove that, it will work as it's normal way.
Note: Other use of ? is to make the previous item optional, which is not the case here, because by using * you are making that part optional already.
Learn more from here : http://www.regular-expressions.info/repeat.html
This question already has answers here:
Get content between two strings PHP
(7 answers)
Closed 4 years ago.
I am trying to replace the content between two words using php. The content between the two words is different so I can't use tradition str_replace. I want to replace the content between two words for example:
I would like to replace **some string of text** between two words
change to:
I would like to replace between two words
You can see that I removed all the wording between "some" and "text". Again I cannot use regular str_replace because the text between the two words may differ. For example it may say:
I would like to replace **some words of text** between two words
change to:
I would like to replace between two words
The regex is simple: /some .*? text/
Just replace it with the empty string.
According to your question, only the inner part of your string changes. If that is the case it's rather trivial, because you already have the solution: You do not need to replace it, but you just need to not take it over:
$result = substr($string, 0, $startlen) . substr($string, -$endlen);
Probably this helps you to find some more "resolution angles" for such problems.
This question already has answers here:
PHP Preg-Replace more than one underscore
(7 answers)
Closed 1 year ago.
this following code will replaces spaces correctly:
$string = preg_replace("/[[:blank:]]+/", "", $string);
but how can I make it so that it will only replace it if there is more than 2 blank spaces? Because right now it replaces all spaces, I only need it to replace more than one space. I searched on here and see people use totally different preg_replace codes, but it also removes newlines so if the code I posted can just be simply modified to allow more than one blank, that would be great. I remember a while back reading a tutorial where it used something like {2+} in the preg area to match anything with more than two or something but not sure how to make it work correctly.
/[[:blank:]]{2,}/
That will make it replace sequences of two or more.
The php manual has a chapter about repetition/quantifiers.
$string = preg_replace("/[[:blank:]]+/", " ", $string);
Same as yours but replaces all occurrences of spaces with one space.
This question already has answers here:
What regex to use for this
(6 answers)
Closed 4 years ago.
I'm having troube modifying this regex. Right now it matches . or ? but I want to change it to match dot followed by a space. How do I do that?
'('/([.|?])/'
By the way, I need the grouping to stay.
What about this:
(\. |\?)
......
The easiest way would be:
'('/(\. )/'
or, if you want a space or a tab or a new-line:
'('/(\.\s)/'
Note that I only changed the part in the inner parenthesis as that part seems to be the focus of your question.
/\.\s/ should work for matching a dot followed by a space..
note: \s matches any whitespace