PHP Trouble replacing string containing dash and underscore [duplicate] - php

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);

Related

Regex PHP part of string [duplicate]

This question already has answers here:
Variable-length lookbehind-assertion alternatives for regular expressions
(5 answers)
Closed 4 years ago.
I cant get my regexpression to work in php. It works in javascript (vuejs):
(?<=.+: )(.*)
I have this string:
NL: abcdef
and i would like to get
abcdef
Can someone please tell me what i am doing wrong?
There are many ways to solve this using PHP/PCRE, one is to skip the preceding string using \K
[^:]+: \K(.*)
Regex Demo
If you can add an anchor to the beginning of the string, even better: ^[^:]+: \K(.*)

How to remove a whitespace in php even if its not detected [duplicate]

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.

Regex in PHP: How to get word in last part URL? [duplicate]

This question already has answers here:
Regex in Notepad++ : How to get last part of URLs?
(4 answers)
Closed 8 years ago.
I have some lines like this:
/gtrhr/5435-silent-library
/fghf3/435-silent-librar
/gfgr2gf/8768-silent-lib
How to get ONLY words in last part of URL ?
So, the oupout will be:
silent-library
silent-librar
silent-lib
Thank you !
[A-Za-z][A-Za-z\-]+$
The $ enforces the match ends with the end of the line.
See here on Regexr.
You could also go for a more general [a-z][\w\-]+$ but that depends on precisely which characters are valid or not in your inputs.

PHP Unknown Modifier [duplicate]

This question already has answers here:
Unknown modifier '/' error in PHP [duplicate]
(5 answers)
Closed 8 years ago.
Have trouble with this and have looked back at previous answers but still
seem not to be getting it. Here is the code:
preg_replace('/^.*"([http|https]://test.co/v/.*)/embed.*width=".*$/', '$1', $str);
Am getting a Unknown modifier '/' error.
Looking at the previous answers it looks like I have to deliminate the / that is
used in the query so I added a ~ before each forward slash but still the same
error. Am guessing I didn't quite understand it... any suggestions appreciated!
Escape the / in the regex as
preg_replace('/^.*"((?:http|https):\/\/test\.co\/v\/.*)\/embed.*width=".*$/', '$1', $str);
OR
You can use a different delimter eg #
preg_replace('#^.*"((?:http|https:)//test\.co/v/.*)/embed.*width=".*$#', '$1', $str);
Note
You can shorten http|https as https?
Escape the . in .co as \.co

preg_replace does not appear to be working am I missing delimiters [duplicate]

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)

Categories