This question already has answers here:
php regex word boundary matching in utf-8
(4 answers)
Closed 5 years ago.
I would like to replace each occurence of hello with bye in a sentence or paragraph.
$sentence = 'nothello hello hello hello hello hello';
$find = 'hello';
$replace = 'bye';
$str = preg_replace('/(^|[\s])'.$find.'([\s]|$)/', '$1'.$replace.'$2', $sentence);
echo $str;
I want this to echo nothello bye bye bye bye bye but instead I get nothello bye hello bye hello bye.
What am I doing wrong?
I can't use \b because I am using lots of languages.
*Edit
I guess \b can work if you use the u flag.
This the right place to use zero-length assertions called lookahead and lookbehind instead of matching:
$str = preg_replace('/(?<=^|\s)'.$find.'(?=\s|$)/', $replace, $sentence);
//=> bye bye bye bye bye
More on lookarounds in regex
(?=...) is positive lookahead and (?<=...) is positive lookbehind.
You current regex search translate into this:
(^|[\s])hello([\s]|$)
which will match a string start with "hello " -- which match the first hello.
or the string " hello " or end with " hello" (a white space before the hello) which will match the 3rd and the last.
if don't need regex search/replace use str_replace as suggested before. If you need to use regex, use a regex test tool/site like https://regex101.com/ to test your regex more closely.
$sentence = 'nothello hello hello hello hello hello';
$find = 'hello';
$replace = 'bye';
$str = preg_replace('/(^|[\s])'.$find.'([\s]#i|$)/', '$1'.$replace.'$2', $sentence);
echo $str;
you need the "#i" to replace it through the whole string
Related
I want to remove the string enclosed with parethesis and the other string on its right
Input:
Hey (Jude) Hello
Expected Output:
Hey
I can only achieve:
Hey Hello by using this code
$string = "Hey (Jude) Hello";
echo preg_replace("/\([^)]+\)/","",$string);
Any thoughts will be appreciated
$string = "Hey (Jude) Hello";
echo preg_replace("/\([^)]+\). */","",$string]);
The dot(.) Matches any character
The star(*) matches 0 or more of preceding character (aka the dot)
Check here for working example
https://regexr.com/3mb6e
You can try
$string = "Hey (Jude) Hello";
echo preg_replace("/\([^)]+\).+/","",$string);
This question already has answers here:
Remove a part of a string, but only when it is at the end of the string
(8 answers)
Closed 12 months ago.
I'm using rtrim() to remove a part at the end of string, , US in my example:
<?php
$str = "Hello - world, c., US, US";
echo rtrim($str,", US");
?>
Output:
Hello - world, c.
It removed , US, US and i want to remove the last one only and the output should be Hello - world, c., US
How i can do that please?
rtrim() doesn't remove a specific string, it uses the string as a list of characters to remove at the end.
Use a regular expression replacement:
echo preg_replace('/, US$/', '', $str);
The $ anchors the match to the end of the string.
substr + strrpos approach:
$str = "Hello - world, c., US, US";
echo substr($str, 0, strrpos($str, ", US"));
The output:
Hello - world, c., US
Here I have a string, "Hello World! I am trying out regex in PHP!". What I want to do is retrieve string values between a set of characters. In this example, the characters are ** **
$str = "**Hello World!** I am trying out regex in PHP!";
preg_match('#\*\*(.*)\*\*#Us', $str, $match);
echo $match[1];
This will echo out "Hello World!", but I want to echo out several matches:
$str = "**Hello World!** I am trying out **regex in PHP!**";
How would I be able to do so? I tried using preg_match_all() but I don't think I was using it properly, or that it would work at all in this case.
You can use:
$str = "**Hello World!** I am trying out **regex in PHP!**";
preg_match_all('/\*{2}([^*]*)\*{2}/', $str, $m);
print_r($m[1]);
Array
(
[0] => Hello World!
[1] => regex in PHP!
)
Even your regex #\*\*(.*)\*\*#Us should work with this but my suggested regex is little more efficient due to negation based pattern [^*]*
You got 1 match owing to using preg_match.You should use preg_match_all Here is another pattern.It uses word non word match between the delimiters
<?php
$str = "**Hello World!** I am trying out **regex in PHP!**";
$regex='/\*\*([\w\W]*)\*\*/iU';
preg_match_all($regex, $str, $m);
print_r($m[1]);
I suggest you to use a non-greedy form of regex. Because i think you want to match also the contents (text inside **) where the single * resides.
$str = "**Hello World!** I am trying out **regex in PHP!**";
preg_match_all('~\*\*(.*?)\*\*~', $str, $matches);
print_r($matches[1]);
DEMO
I have a question with some patterns I'm trying to do... this is the code I have:
$test = 'this is a simply test';
preg_match_all("/^this is a [a-zA-Z] test$/", $test, $op_string);
print_r($op_string);
I've been trying for this guys, but this doesn't works properly. This should output: simply
The pattern must contains same as $test (the string I mean... it can't contain only [a-zA-Z] because we'll need to find it more exactly as possible).
Thank you so much!
Use quantifier + to match 1 or more letters:
$test = 'this is a simply test';
preg_match_all('/^this is a [a-zA-Z]+ test$/', $test, $op_string);
You're using [a-zA-Z] which will only match a single letter.
try this:
$re = "/^this is a ([a-zA-Z\\s]+) test$/m";
$str = "this is a simply test";
preg_match_all($re, $str, $matches);
var_dump($matches[1]); // here you get match word or word set
live demo
output:
array (size=1)
0 => string 'simply' (length=6)
How can I get a portion of the string from the beginning until the first non-alphabetic character?
Example strings:
Hello World
Hello&World
Hello5World
I'd like to get the "Hello" part
You need to use the preg_split feature.
$str = 'Hello&World';
$words = preg_split('/[^\w]/',$str);
echo $words[0];
You can access Hello by $words[0], and World by $words[1]
You can use preg_match() for this:
if (preg_match('/^([\w]+)/i', $string, $match)) {
echo "The matched word is {$match[1]}.";
}
Change [\w]+ to [a-z]+ if you do not want to match the 5 or any numeric characters.
Use preg_split. Split string by a regular expression
If you only want the first part, use preg_match:
preg_match('/^[a-z]+/i', $str, $matches);
echo $matches[0];
Here's a demo.
Use preg_split to get first part of alpha.
$array = preg_split('/[^[:alpha:]]+/', 'Hello5World');
echo $array[0];