So this is a preg_replace associated question i guess,
I have a string with multiple repeating patterns
they all formated as:
some string :22: more text :12: etc
how do i replace the ":" around them with some different char?
You can do something like this:
$string = 'some string :22: more text :12: etc';
$regex = '/:(\d+):/';
$newString = preg_replace($regex, "#$1#", $string);
Note: You have to replace the '#' in the second parameter with the char you want (also different chars before and after the numbers).
Sbustitudes _ for : around numbers:
preg_replace('/:(\d+):/', '_$1_', 'some string :22: more text :12: etc');
EDIT: Misunderstood original question. However, is still a flexible option:
$result = str_replace(":22:", "tag", "some string :22: more text :12: etc");
$result = str_replace(":12:", "other_tag", $result);
Replace the ? character with your replacement character.
Related
This is my variable to be altered:
$last = 'Some string 1 foobar'
and my replace statement
$last = str_replace(['1', '2'], '', $last);
and finally the output
Some string foobar
How do i get rid of the whitespace in between 'string' and 'foobar', my initial thought was that in my str_replace statement using '' as the replacement would also remove the whitespace but it doesnt.
To clarify I want to know how to make it Some string foobar and not Some stringfoobar.
A regular expression based approach is more flexible for such stuff:
<?php
$subject = 'Some string 1 foobar';
var_dump(preg_replace('/\d\s?/', '', $subject));
The output of above code is: string(18) "Some string foobar"
What does that do, how does it work? It replaces a pattern, not a fixed, literal string. Here the pattern is: any digit (\d) along with a single, potentially existing white space character (\s?).
A different, alternative approach would be that:
<?php
$subject = 'Some string 1 foobar';
var_dump(preg_replace('/(\s\d)+\s/', ' ', $subject));
This one replaces any sequence consisting of one or more occurrences of a digit preceded by a white space ((\s\d)+) along with a single white space by a single white blank character.
If you do not want to use preg_replace then you can do something like this.
$result = 'Some string 1 foobar';
$result = str_replace(['1', '2'], '', $result);
$result = str_replace(' ', ' ', $result);
However I have to admit that I like preg_replace solution more. Not sure about the benchmark though.
i've scraped a html string from a website. In this string it contains multiple strings like color:#0269D2. How can i make str_replace code which replace this string with another color ?
For instance something like this just looping through all color:#0269D in the fulltext string variable?
str_replace("color:#0269D","color:#000000",$fulltext);
you pass array to str_replace function , no need to use loop
$a= array("color:#0269D","color:#000000");
$str= str_replace($a,"", $string);
You have the right syntax. I would add a check:
$newText = str_replace("color:#0269D", "color:#000000", $fulltext, $count);
if($count){
echo "Replaced $count occurrences of 'color'.";
}
This code might be too greedy for what you're looking to do. Careful. Also if the string differs at all, for example color: #0269D, this replacement will not happen.
’str_replace’ already replaces all occurrences of the search string with the replacement string.
If you want to replace all colors but aren't sure which hexcodes you'll find you could use preg_replace to match multiple occurrences of a pattern with a regular expression and replace it.
In your case:
$str = "String with loads of color:#000000";
$pattern = '/color ?: ?#[0-9a-f]{3,6}/i';
$replacement = "color:#FFFFFF";
$result = preg_replace($pattern, $replacement, $str);
I have a doubt in regular expression. I want to replace a particular text which is present in 2 characters in a string.
Example:
$my_string = "newtext!234##random_text##weludud";
$new_text = 'replaced_text";
In myabove string I want to replace the text between my characters ##. So in the above string I want to replace random_text with replaced_text.
So my output will be newtext!234##replaced_text##weludud
If ## text ## appears only once in the string, you can use explode.
$my_string = "newtext!234##random_text##weludud";
$new_text = 'replaced_text';
$var = explode('##',$my_string); //create an array with 3 parts, the middle one being the text to be replaced
$var[1]=$new_text;
$my_string=implode('##',$var);
(?<=##)(?:(?!##).)*(?=##)
Try this.Replace by replace_text.See demo.
http://regex101.com/r/sU3fA2/40
$my_string = "newtext!234##random_text##weludud";
$replace = 'replaced_text';
$replaced_text = preg_replace('#(#)(.*)(#)#si', "$1$replace$3", $my_string);
echo $replaced_text;
Working demo
I need to extract a project number out of a string. If the project number was fixed it would have been easy, however it can be either P.XXXXX, P XXXXX or PXXXXX.
Is there a simple function like preg_match that I could use? If so, what would my regular expression be?
There is indeed - if this is part of a larger string e.g. "The project (P.12345) is nearly done", you can use:
preg_match('/P[. ]?(\d{5})/',$str,$match);
$pnumber = $match[1];
Otherwise, if the string will always just be the P.12345 string, you can use:
preg_match('/\d{5}$/',$str,$match);
$pnumber = $match[0];
Though you may prefer the more explicit match of the top example.
Try this:
if (preg_match('#P[. ]?(\d{5})#', $project_number, $matches) {
$project_version = $matches[1];
}
Debuggex Demo
You said that project number is 4 of 5 digit length, so:
preg_match('/P[. ]?(\d{4,5})/', $tring, $m);
$project_number = $m[1];
Assuming you want to extract the XXXXX from the string and XXXXX are all integers, you can use the following.
preg_replace("/[^0-9]/", "", $string);
You can use the ^ or caret character inside square brackets to negate the expression. So in this instance it will replace anything that isn't a number with nothing.
I would use this kind of regex : /.*P[ .]?(\d+).*/
Here is a few test lines :
$string = 'This is the P123 project, with another useless number 456.';
$project = preg_replace('/.*P[ .]?(\d+).*/', '$1', $string);
var_dump($project);
$string = 'This is the P.123 project, with another useless number 456.';
$project = preg_replace('/.*P[ .]?(\d+).*/', '$1', $string);
var_dump($project);
$string = 'This is the P 123 project, with another useless number 456.';
$project = preg_replace('/.*P[ .]?(\d+).*/', '$1', $string);
var_dump($project);
use explode() function to split those
I hate regular expressions and I was hoping someone could help with a regualar expression to be used with preg_replace.
I want to strip unwanted characers from a string to return just a numeric value using preg_replace.
The string format could be as follows:
SOME TEXT £100
£100 SOME TEXT
SOME TEXT 100 SOME TEXT
Many thanks
$NumericVal = preg_replace("/[^0-9]/","",$TextVariable);
the ^ inside the [ ] means anything except the following
Edit
removed superfluous +
$l = preg_replace("/[^A-Z0-9a-z\w ]/u", '', $l);
Works witch UTF-8, allow only A-Z a-z 0-9 łwóc... etc
preg_replace('/[^0-9]/','',$text);
Try this:
preg_replace("/\D+/", "", "SOME TEXT £100")
You can also use preg_match to get the first number:
preg_match("/\d+/", "SOME TEXT £100", $matches);
$number = $matches[0];