This question already has answers here:
Regex to replace surrounded word multiple times
(3 answers)
PHP Regex replace all instances of a character in similar strings
(4 answers)
PHP replace all occurences with preg_replace_callback
(1 answer)
php regex preg_replace_callback fails to handle mutliple lines
(1 answer)
Replace unwanted characters inside opening HTML tag only
(4 answers)
Closed 17 days ago.
Hi I have trouble understanding the preg_replace rules in php.
I have a string:
<fa:foo r:attr="ddd">fa:foo</fa:foo>,
and I am looking for a way to replace the colon by underscores only within each set of < and >
and reach following result:
<fa_foo r_attr="ddd">fa:foo</fa_foo>
Related
This question already has answers here:
Returning a string after the second occurrence of a character and before the last occurrence of the same character with php
(3 answers)
Closed 8 months ago.
eg. this string:
$foo = 'this_is_a_string';
Now I want to extract the string that comes after the second _ symbol, no matter how long the string is or how many _ symbols it contains.
this should be the result:
"a_string"
<?php
$foo = 'this_is_a_string';
var_dump(substr($foo,stripos($foo,'_',stripos($foo,'_')+1)+1));
?>
This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 2 years ago.
How can I convert all multiple white space inside any given HTML element to a single space using regex and preg_replace in php?
Eg: <div class="myClass" jsaction="UjQMac:.CLIENT" data-id="3739" >Edit</div>
Cleaned: <div class="myClass" jsaction="UjQMac" data-id="3739">Edit</div> All multiple spaces removed and only single spaces retained. Also, the > is replaced with a >
I've been trying unsuccessfully with this regex \<(\s+)\>. Can you help?
Edit:
The regex (?:(\s{2,})|(\s>)) from the answer below works fine, but does not match only between < & >
This will do it: (?:(\s{2,})|(\s>))
It matches any whitespace character that appears 2x or more often and also > with a leading .
See: https://regex101.com/r/NN9YUU/2/
This question already has answers here:
How to I remove special characters and spaces on a textfield using PHP
(3 answers)
Closed 6 years ago.
I need to find and replace a word like "test", but even if it has none-alphabetic chars in it, like:
test
t e s t
t.e.s.t
t-e-s-t
t.e-s(t
etc.
Any ideas how to do this in php? Perhaps with preg_replace?
Try this:
preg_replace("/t\W?e\W?s\W?t/", "something", $input_array);
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.
This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 3 years ago.
I need this php code changed to preg_match please
if(eregi("someurl",$data))
if (preg_match('~someurl~i', $data)). Delimiters can be any single character — I've used ~ instead of / used in examples on PHP site, because if you're putting URL in there, you'd have to escape /. i after the second delimiter is a flag triggering case-insensitivity — PCRE doesn't have separate functions for that.