regex does not match if string contains spaces [duplicate] - php

This question already has an answer here:
Optional Whitespace Regex
(1 answer)
Closed 2 years ago.
I'm using regex js\w*=\"[^\"]+\" to match and replace tags in HTML that begin with js like jsname="name" jscontroller="somecontroller" etc.
Problem is, if there are spaces before and after the = sign it matches nothing. How can this be solved?
This matches fine jsname="name" jscontroller="somecontroller"
This does not match but should: jsname = "name" jscontroller = "somecontroller"
Can you help?

Use js\w*\s*=\s*\"[^\"]+\"

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(.*)

PHP Trouble replacing string containing dash and underscore [duplicate]

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

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.

Regex not working as expected [duplicate]

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

Categories