I'm trying to replace 0-1. with 0-1.<br> how do i do that?
update:
Sorry for my vague question. You guys misunderstood me. '0-1.' is the pattern i want to replace, which means the pattern should be like `"/(\d)+(-)*(\d)*\./"` and the string may be '1.' '0-1.' or something that expression could represent
You can use a standard PHP function:
str_replace('0-1.', '0-1.<br>', $yourString);
How about:
preg_replace("/(\d+(?:-\d+)?\.)/", "$1<br>", $string);
You can use preg_replace like this:
preg_replace("/(0-1\.)/", "$1<br>", $string);
or, as you know the substitution already:
preg_replace("/0-1\./", "0-1.<br>", $string);
Related
Hey I'm filtering a string and want it to go from:
512MBGDDR5videogeheugen
To:
512MB
So I tried php preg replace and did this:
$filterString = preg_replace("/[^0-9]+(KB|MB|GB)/", "", $string);
Does anyone know a way to solve this?
THANKS FOR THE RESPONSE!
Instead of replacing you can get your match like this.
preg_match("/^([0-9]+(KB|MB|GB))/", $string, $results);
$filterString = $results[0];
You can also use T-Regx library that has automatic delimiters:
pattern('^[0-9]+(KB|MB|GB)')->match($string)->all();
I'm actually not home so I tried a javascript regex, but I think it should work:
$filtered = preg_replace('^([0-9]+(KB|MB|GB))(.+)$','$1',$string)
Well, I'm trying to do something very simple but turns out it's pretty complicated.
I have this string which is -
post/6
And for example and I want to replace it with -
post/view/6
so I tried to replace it using the following pattern -
/post/[1-9-a-z]/
And the replacement pattern -
view/$0
But for some reason, the string that's returned is this string -
/view/post/6
Maybe you guys know the solution for this?
I greatly appreciate your help.
You could simply use str_replace() no regex is required:
echo str_replace('post/', 'post/view/', $str);
you can use
Search Pattern
~post/([1-9-a-z])~
with Replacement Pattern
post/view/$1
This is expected result
$0 represents the complete string that has been matched i.e post/6 in your case
You can use lookaround
preg_replace('/(?<=post)(?=\/[\da-z])/',"/view",$txt);
OR
preg_replace('/(post)(\/[\da-z])/',"$1/view$2",$txt);
would like to replace all occurence of, double quote included
"http://somebunchofchar"
to
"link"
I came up with preg_replace("/\"http:\/\/.\"/i", "\"link\"", $string);
Just add an asterisk and question mark after dot
preg_replace("/\"http:\/\/.*?\"/i", "\"link\"", $string);
$string = preg_replace('#"http://.+"#', '"link"', $string);
You can use:
preg_replace('~"http://[^"]*"~i', '"link"', $string);
Just look here:
http://regexlib.com/DisplayPatterns.aspx?cattabindex=1&categoryId=2&AspxAutoDetectCookieSupport=1
how to match an URL with the correct pattern; than use preg_replace with the particular regexp pattern ;-) (you can add those quotes at the start and end to the pattern yourself quite easily) :-)
Let say I have the following string:
getPasswordLastChangedDatetime
How would I be able to split that up by capital letters so that I would be able to get:
get
Password
Last
Changed
Datetime
If you only care about ASCII characters:
$parts = preg_split("/(?=[A-Z])/", $str);
DEMO
The (?= ..) construct is called lookahead [docs].
This works if the parts only contain a capital character at the beginning. It gets more complicated if you have things like getHTMLString. This could be matched by:
$parts = preg_split("/((?<=[a-z])(?=[A-Z])|(?=[A-Z][a-z]))/", $str);
DEMO
Asked this a little too soon, found this:
preg_replace('/(?!^)[[:upper:]]/',' \0',$test);
For instance:
(?:^|\p{Lu})\P{Lu}*
No need to over complicated solution. This does it
preg_replace('/([A-Z])/',"\n".'$1',$string);
This doens't take care of acronyms of course
Use this: [a-z]+|[A-Z][a-z]* or \p{Ll}+|\p{Lu}\p{Ll}*
preg_split("/(?<=[a-z])(?=[A-Z])/",$password));
preg_split('#(?=[A-Z])#', 'asAs')
I'm new to preg_replace() and I've been trying to get this to work, I couldn't so StackOverflow is my last chance.
I have a string with a few of these:
('pm_IDHERE', 'NameHere');">
I want it to be replaced with nothing, so it would require 2 wildcards for NameHere and pm_IDHERE.
But I've tried it and failed myself, so could someone give me the right code please, and thanks :)
Update:
You are almost there, you just have to make the replacement an empty string and escape the parenthesis properly, otherwise they will be treated as capture group (which you don't need btw):
$str = preg_replace("#\('pm_.+?', '.*?'\);#si", "", $str);
You probably also don't need the modifiers s and i but that is up to you.
Old answer:
Probably str_replace() is sufficient:
$str = "Some string that contains pm_IDHERE and NameHere";
$str = str_replace(array('pm_IDHERE', 'NameHere'), '', $str);
If this is not what you mean and pm_IDHERE is actually something like pm_1564 then yes, you probably need regular expressions for that. But if NameHere has no actual pattern or structure, you cannot replace it with regular expression.
And you definitely have to explain better what kind of string you have and what kind of string you have want to replace.