How to Remove Text Between brackets with php [duplicate] - php

This question already has answers here:
Remove Text Between Parentheses PHP
(7 answers)
Closed 4 years ago.
How to remove text Between brackets.
For exp.
$str = 'Aylmers(test, test2), Ancaster(Clandeboye, Bluevale)';
i want to get as
$str = 'Aylmers, Ancaster';

Try this:
$str = 'Aylmers(test, test2), Ancaster(Clandeboye, Bluevale)';
echo preg_replace("/\([^)]+\)/","",$str );
output:
Aylmers, Ancaster
Regex

This should do it:
`<?php`
`$string = "Stay 01 (Remove 01), Stay 02 (Remove 02)";`
`echo preg_replace("/\([^)]+\)/","",$string); // 'ABC '`

If also want to remove nested data in brackets. You can use:
$str = 'Aylmers(test, test2), Ancaster(Clandeboye, Bluevale)';
echo preg_replace("/\(([^()]*+|(?R))*\)/","", $str);
//output:
//Aylmers, Ancaster
Explanation:
/ - Opening delimiter
( - Match opening parenthesis
[^)]+ - Match character that is not a closing parenthesis
) - Match closing parenthesis
/ - Closing delimiter

Related

Strip commas from a string in PHP [duplicate]

This question already has an answer here:
Cannot work out a php str_replace() to remove comma [closed]
(1 answer)
Closed 2 years ago.
I have the following string:
Input:
$str = "I want to remove only comma from this string, how ?";
I want to remove commas from $str, I'm new in programming and I don't understand how regex works.
use str_replace.
Example
$str = "I want to remove only comma from this string, how ?";
$str = str_replace(",", "", $str);
Explaination
As you can see there is 3 arguments we pass in str_replace
"," => this one is what you want to replace
"" => this one is value that will replace first argument value. we pass blank so it will replace comma to blank
this one is string where you want to replace.
Regex: (?<!\d)\,(?!\d)
(\,|\.) for matching exact either , or .
(?!\d) should not contains digits ahead.
(?<!\d) should not contains digit behind.
PHP code:
<?php
$str = "I want to remove only comma from this string, how. ? Here comma and dot 55,44,100.6 shouldn't be removed";
echo preg_replace("/(?<!\d)(\,|\.)(?!\d)/", "", $str);
Output:
I want to remove only comma from this string how ? Here comma 55,44,100 shouldn't be removed

PHP only leave text in between particular characters

I have many strings that look like this:
[additional-text Sample text...]
There is always an opening bracket + additional-text + single space and a closing bracket in the end and I need to remove all of them, so that the final string would look like this:
Sample text...
Any help or guidance is much appreciated.
You can use this o get all matches within a text block:
preg_match_all("/\[additional-text (.*?)\]/",$text,$matches);
all your texts will be in $matches[1]. So that will be:
$text = "[additional-text Sample text...]dsfg fgfd[additional-text Sample text2...] foo bar adfd as ff";
preg_match_all("/\[additional-text (.*?)\]/",$str,$matches);
var_export($matches[1]);
Get the substring you want to keep as a captured group:
^\[\S+\s([^]]+)\]$
Now in the replacement, use the only captured group, \1.
Demo
You can use:
$re = '/\[\S+\s|\]/';
$str = "[additional-text Sample text...]";
$result = preg_replace($re, '', $str);
//=> Sample text...
RegEx Demo
Use substr to remove the first 17 characters. Use regex to remove the last two:
$val = '[additional-text Sample text...]';
$text = preg_replace('#\]$#', '', substr($val, 17));
you can also do this
$a = '[additional-text Sample text...]';
$a= ltrim($a,"[additional-text ");
echo $a= rtrim($a,"]");
There is no need in regex, use substr:
$s = "[additional-text Sample text...]";
echo substr($s, 17, strlen($s)-18);
Where 17 is the length of [additional-text and 18 is the same + 1 for the last ].
See PHP demo
A regex solution is also basic:
^\[additional-text (.*)]$
or - if there can be no ] before the end:
^\[additional-text ([^]]*)]$
And replace with $1 backreference. See the regex demo, and here is a PHP demo:
$result = preg_replace('~^\[additional-text (.*)]$~', "$1", "[additional-text Sample text...]");
echo $result;
Pattern details:
^ - start of string
\[ - a literal [
additional-text - literal text
(.*) - zero or more characters other than a newline as many as possible up to
]$ - a ] at the end of the string.

Remove last characters of strings using regex [duplicate]

This question already has answers here:
PHP substring extraction. Get the string before the first '/' or the whole string
(14 answers)
Closed 12 months ago.
I need to find a way in PHP to remove the last portions of 2 strings using regex's. This way once they are stripped of the extra characters I can find a match between them. Here is an example of the type of string data I am dealing with:
categories_widget-__i__
categories_widget-10
So I would like to remove:
-__i__ from the first string
-10 from the second string
Thanks in advance.
(.*)-
This simple regex can do your job if - is the splitting criteria
See demo.
http://regex101.com/r/rX0dM7/7
$str1 = "categories_widget-__i__";
$str2 = "categories_widget-10";
$arr1 = explode("-", $str1);
$arr2 = explode("-", $str2);
echo $arr1[0];
echo $arr2[0];
Is the last occurrence of a hyphen the only thing that's important? If so you don't need regex:
$firstPart = substr($str, 0, strrpos($str, '-'));
ยป example
You could try the below code to remove all the characters from - upto the last.
<?php
$text = <<<EOD
categories_widget-__i__
categories_widget-10
EOD;
echo preg_replace("~-.*$~m","",$text);
?>
Output:
categories_widget
categories_widget
- matches the literal - symbol. And .* matches any character following the - symbol upto the end of the line. $ denotes the end of a line. By replacing all the matched characters with an empty string would give you the desired output.

Get rid of text in between () in PHP [duplicate]

This question already has answers here:
Remove Text Between Parentheses PHP
(7 answers)
Closed 9 years ago.
Say you have a string like this:
This is a string (with parenthesis stuff)
How would you change that to
This is a string
?
Replace it:
preg_replace('/\(.*?\)/', '', $str);
Use a regex.
Replace \([^)]*\) with the empty string.
Note: If there's more than one pair of parenthesis this will replace the innermost one.
try this
$string = "This is a string (with parenthesis stuff)";
echo preg_replace("/\([^)]+\)/","",$string); // 'ABC '
or you can do this also
$str = "This is a string (with parenthesis stuff)";
$str = trim(preg_replace('/\s*\([^)]*\)/', '', $str));

Trim multiple characters using php [duplicate]

This question already has answers here:
PHP ltrim behavior with character list
(2 answers)
Closed 12 months ago.
How to trim multiple characters at begin and end of string.
string should be something like {Hello {W}orld}.
i want to trim both { and } at begin and end.
don't want to use multiple trim function.
Use the optional second argument to trim which allows you to specify the list of characters to trim:
<?php
$str = "{Hello {W}orld}";
$str = trim($str, "{}");
echo "Trimmed: $str";
Output:
Trimmed: Hello {W}orld
Is there always a character at the beginning and at the end, that you want to remove? If so you could just use the following:
<?php
$string = '{Hello {W}orld}';
echo substr( $string, 1, strlen( $string )-2 );
?>
See: http://codepad.org/IDbG6Km2

Categories