This question already has answers here:
Delimiter must not be alphanumeric or backslash and preg_match
(7 answers)
Closed 6 years ago.
I know is something pretty simple for someone who understand well preg_replace but I can't understand how to remove part of url. I have form where user should enter a token. The problem is that some users submitted whole url with token this kind of url
https://example.com/page/token
and
http://example.com/page/token
I want when user submit the form to check if he/she inserted whole url and if is to remove everything and leave only token there
I've tried simple preg_replace like this
$result = preg_replace('https://example.com/page/', '', $str);
But error says
Warning: preg_replace(): Delimiter must not be alphanumeric or backslash
Then I've tried to escape those backslashes with \ etc .. but same error occurred.
Any help is appreciated.
Your preg_replace() function should be like this:
$result = preg_replace('$(https|http)://example.com/page/$', '', $str);
You can use $ as a delimiter.
Related
This question already has answers here:
PHP regular expressions: No ending delimiter '^' found in
(2 answers)
Closed 6 years ago.
I have tried, read and searched to to avail.
I just need to replace -m_ with -b_ in a string containing an image name / location.
For instance:
I just want to replace;
some-image-name-hyphenated-m_15235101.jpg
with
some-image-name-hyphenated-b_15235101.jpg
Easy right?
$mediumimage = 'some-image-name-hyphenated-b_15235101.jpg';
I tried
$biggerimage = preg_replace("-m_", "-b_", $mediumimage);
and
$biggerimage = preg_replace('-m_', '-b_', $mediumimage);
Also backslashing along with other tries resulting from searches.
Warning: preg_replace(): No ending delimiter '-' found
I don't feel well right now...
I beseech anyone who is smarter than me e.g..... any one here.
Try use $biggerimage = str_replace("-b_", "-m_", $mediumimage);
This question already has answers here:
Regular expression to remove one parameter from query string
(9 answers)
Closed 6 years ago.
I have an url which looks like page.php?stuff=stuff&stuff=stuff&order=sometext&page=number
and I want to remove the "order=sometext" part. I'm using this php code :
$link = $_SERVER['REQUEST_URI'];
$re = "/&order=/";
$link = preg_replace($re, "", $link);
And I'm looking for the correct pattern that should be $re to clear the "order=sometext" from the url.
The "&page=number" part has to be kept when present, but isn't always there.
Any ideas ?
Thanks in advance !
The regex should be
(&|\?)order=[^&]+
(&|\?): the order could be first in the list, so we are removing either & or ?.
Actually I made it work using /&order=[^&]*/. :)
This question already has answers here:
How do I strip all spaces out of a string in PHP? [duplicate]
(4 answers)
Closed 7 years ago.
I'm trying to remove a whitespace from a string, but this whitespace its not detected, i've tried so many solutions like str_replace(), preg_replace() htmlentities() an html_entity_decode() but its not working because i can't detect this whitespace even if it's really present.
So any ideas please ?
An example : 7412 148 ==> The goal is : 7412148
PS : i've tried to convert it to INT (not working) !
Thanks
Let's use preg_replace to replace/remove everything that is not a digit.
$str = "1234 5657"; // this is an example
$number = preg_replace("/[^0-9]/","",$str);
print $number;
Use this with the string that your function returns and let us know if it works.
This question already has answers here:
parse youtube video id using preg_match [duplicate]
(10 answers)
Closed 8 years ago.
I have the following regex which works fine in a regex editor but when I pull it together in PHP i am getting and Unknown modifier '(' error come up.
preg_replace("(\[LINK\])(\S*)(\[\/LINK])", "<a>href=\'$2\'>$2</a>", $xtext);
This is my first question on SO so I hope I have given enough information. From my research I believe I am missing delimiters but tried ~ at the start and the end of the search pattern and still does not seem to work.
try this
preg_replace("/(\[LINK\])(\S*)(\[\/LINK])/", "<a>href=\'$2\'>$2</a>", $xtext);
Note the delimiters
Just try with:
$input = 'foo [LINK]http://google.com[/LINK] bar';
$output = preg_replace('/\[LINK\](.*?)\[\/LINK\]/', '$1', $input);
Output:
string 'foo http://google.com bar' (length=57)
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.