PHP remove string enclosed a parenthesis and after it - php

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

Related

PHP replace part of string with link based on pattern

I would like to replace all words starting with 3ABC with an link including the found word. For example:
teststring 3ABCJOEDKLSZ2 teststring hello test
Output would be:
test string <a href='https://google.com/search/3ABCJOEDKLSZ2'>3ABCJOEDKLSZ2</a> teststring hello test
The substring I am looking for is always starting with 3ABC everything after that is dynamic.
You can use php's preg_replace function to match 3ABC followed by 0 or more characters that is not whitespace and then use the match in your code:
$literal = "teststring 3ABCJOEDKLSZ2 teststring hello test";
$formatted = preg_replace("/3ABC\S*/", '\0', $literal);
echo $formatted;
Fiddle: Live Demo
<?php
function makeLink($string)
{
$pattern='/^3ABC[\w\d]+$/';
$url='https://google.com/search/';
$result=preg_replace($pattern, $url.$string ,$string);
return $result;
}
echo makeLink('3ABCHJDGIFD');
?>
Like this?
http://php.net/manual/en/function.preg-replace.php
the pattern will match any digit or word character after 3ABC.

preg_replace() not working for repeating words [duplicate]

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

Twitter handle regular expression PHP [duplicate]

i'm not very firm with regular Expressions, so i have to ask you:
How to find out with PHP if a string contains a word starting with # ??
e.g. i have a string like "This is for #codeworxx" ???
I'm so sorry, but i have NO starting point for that :(
Hope you can help.
Thanks,
Sascha
okay thanks for the results - but i did a mistake - how to implement in eregi_replace ???
$text = eregi_replace('/\B#[^\B]+/','\\1', $text);
does not work??!?
why? do i not have to enter the same expression as pattern?
Match anything with has some whitespace in front of a # followed by something else than whitespace:
$ cat 1812901.php
<?php
echo preg_match("/\B#[^\B]+/", "This should #match it");
echo preg_match("/\B#[^\B]+/", "This should not# match");
echo preg_match("/\B#[^\B]+/", "This should match nothing and return 0");
echo "\n";
?>
$ php 1812901.php
100
break your string up like this:
$string = 'simple sentence with five words';
$words = explode(' ', $string );
Then you can loop trough the array and check if the first character of each word equals "#":
if ($stringInTheArray[0] == "#")
Assuming you define a word a sequence of letters with no white spaces between them, then this should be a good starting point for you:
$subject = "This is for #codeworxx";
$pattern = '/\s*#(.+?)\s/';
preg_match($pattern, $subject, $matches);
print_r($matches);
Explanation:
\s*#(.+?)\s - look for anything starting with #, group all the following letters, numbers, and anything which is not a whitespace (space, tab, newline), till the closest whitespace.
See the output of the $matches array for accessing the inner groups and the regex results.
#OP, no need regex. Just PHP string methods
$mystr='This is for #codeworxx';
$str = explode(" ",$mystr);
foreach($str as $k=>$word){
if(substr($word,0,1)=="#"){
print $word;
}
}
Just incase this is helpful to someone in the future
/((?<!\S)#\w+(?!\S))/
This will match any word containing alphanumeric characters, starting with "#." It will not match words with "#" anywhere but the start of the word.
Matching cases:
#username
foo #username bar
foo #username1 bar #username2
Failing cases:
foo#username
#username$
##username

preg_replace not ignoring [] from string

This is my code :
$string = "Hello this is my text [readmore] and this is remaining text";
echo preg_replace("[readmore]","",$string);
This is my expected output :
Hello this is my text and this is remaining text
This is my actual output :
Hello this is my text [] and this is remaining text
Question is simple how to get ride of "[]" too ?
You need to escape [ & ]. Try below regexp,
preg_replace("/\[([^\[\]]++|(?R))*+\]/", "", $string);
OUTPUT:
Hello this is my text and this is remaining text
CodePad Demo.
You need to escape the brackets for use in regular expressions. Also, you might want to look at the manual for preg_replace, since you left off the / that needs to be around your regex.
$string = "Hello this is my text [readmore] and this is remaining text";
echo preg_replace("/\[readmore\]/","",$string);
Escape the '[' and ']' characters:
This script:
$string = "Hello this is my text [readmore] and this is remaining text";
echo preg_replace("[\\[readmore\\] ]","",$string);
This result (I tested this):
Hello this is my text and this is remaining text
Use str_replace
$string = "Hello this is my text [readmore] and this is remaining text";
echo str_replace("[readmore]","",$string);
Output
Hello this is my text and this is remaining text

preg_match all words start with an #?

i'm not very firm with regular Expressions, so i have to ask you:
How to find out with PHP if a string contains a word starting with # ??
e.g. i have a string like "This is for #codeworxx" ???
I'm so sorry, but i have NO starting point for that :(
Hope you can help.
Thanks,
Sascha
okay thanks for the results - but i did a mistake - how to implement in eregi_replace ???
$text = eregi_replace('/\B#[^\B]+/','\\1', $text);
does not work??!?
why? do i not have to enter the same expression as pattern?
Match anything with has some whitespace in front of a # followed by something else than whitespace:
$ cat 1812901.php
<?php
echo preg_match("/\B#[^\B]+/", "This should #match it");
echo preg_match("/\B#[^\B]+/", "This should not# match");
echo preg_match("/\B#[^\B]+/", "This should match nothing and return 0");
echo "\n";
?>
$ php 1812901.php
100
break your string up like this:
$string = 'simple sentence with five words';
$words = explode(' ', $string );
Then you can loop trough the array and check if the first character of each word equals "#":
if ($stringInTheArray[0] == "#")
Assuming you define a word a sequence of letters with no white spaces between them, then this should be a good starting point for you:
$subject = "This is for #codeworxx";
$pattern = '/\s*#(.+?)\s/';
preg_match($pattern, $subject, $matches);
print_r($matches);
Explanation:
\s*#(.+?)\s - look for anything starting with #, group all the following letters, numbers, and anything which is not a whitespace (space, tab, newline), till the closest whitespace.
See the output of the $matches array for accessing the inner groups and the regex results.
#OP, no need regex. Just PHP string methods
$mystr='This is for #codeworxx';
$str = explode(" ",$mystr);
foreach($str as $k=>$word){
if(substr($word,0,1)=="#"){
print $word;
}
}
Just incase this is helpful to someone in the future
/((?<!\S)#\w+(?!\S))/
This will match any word containing alphanumeric characters, starting with "#." It will not match words with "#" anywhere but the start of the word.
Matching cases:
#username
foo #username bar
foo #username1 bar #username2
Failing cases:
foo#username
#username$
##username

Categories