PHP: regular expression to remove bracket codes - php

I am trying to make a function to remove all the bracket codes but it doesn't seem to be working,
function anti_code($content)
{
# find the matches and then remove them
$output = preg_replace("/\[a-z\s+\]/is", "", $content);
# return the result
return $output;
}
I want these codes to be removed in the output,
Agro[space]terrorism
Agro[en space]terrorism
so that I can get
Agroterrorism
I must be something wrong in my regular expression! Please let me know. Thanks.

You escaped the [], but didn't add a second set of unescaped [] to designate a character class. Also, the s is not necessary if you're not using the . metacharacter in your regex.
Try this:
/\[[a-z\s]+\]/i
If you don't care what's between the square brackets and just want to remove everything contained in them, this will do:
/\[[^]]+\]/i

Try \[[a-z\s]+\] It will capture brackets and all contents

Related

PHP preg_match exact match and get whats inside brackets

Lets say I have the following string:
"**link(http://google.com)*{Google}**"
And I want to use preg_match to find the EXACT text **link(http://google.com) but the text inside the brackets changes all the time. I used to use:
preg_match('#\((.*?)\)#', $text3, $match2);
Which would get what is inside the brackets which is good but if I had: *hwh(http://google.com)** it would get whats inside of that. So how can i get whats inside the brackets if, in front of the brackets has **link?
~(?:\*\*link\(([^\)]+)\))~ will match contents in the brackets for all inputs that look like **link(URL) but do not contain extra ) inside URLs. See the example on Regexr: http://regexr.com/3en33 . The whole example:
$text = '"**link(http://google.com)*{Google}**"
**link(arduino.cc)*{official Arduino site}';
$regex = '~(?:\*\*link\((?<url>[^)]+))~';
preg_match_all($regex, $text, $matches);
var_dump($regex, $matches['url']);
Here
preg_match("/\*\*link\((\D+)\)/",$text,$match);
Use a lookbehind operator ?<=
(?<=\*\*link)\((.*)\) gives you what's inside braces if the text behind is **link
Update:
Here's a PHP example
Here's a regex example

How to define regex in php to check strings

I've been using following regex to search the string containing "function(...){}":
/(?<=:)"function\((?:(?!}").)*}"/
The string might also contain "function ()" - space between function and bracket "()"
how can regex be defined to check "function()..." or " function ()..."?
Just add an optional space in the appropriate place:
/(?<=:)"function *\((?:(?!}").)*}"/
" *" matches zero or more spaces.
Try this:
/(?<=:)"function\s*\((?:(?!}").)*}"/
here \s* means zero or more ...occurrence of space...
(you are missing { to match?)
/(?<=:)"function[ ]*\((?:(?!}").)*}"/
This would do it.

preg replace string containing square brackets

I have a string like this: [name-123456].
I am attempting to replace the match of this string with some predefined strings. Here is what I have so far:
preg_replace('~\['.$string_to_be_replaced.'\]~', $code_to_replace_it_with, $content);
Currently this throws an error, I couldn't find out how to remove the square brackets (even though they are part of the string). How do I make sure those get removed in a regex so that [name-123456] gets replaced with stringofcode?
EDIT:
$string_to_be_replaced = preg_quote($string_to_be_replaced, "~");
$content = preg_replace('~\['.$string_to_be_replaced.'\]~', $str_to_replace_with, $content);
this simply returns [name-123456] :p
A vardump produces: string(16) "\[name\-123456\]"
Your first problem is likely that you didn't assign the result back:
$content = preg_replace('~...~', $rpl.., $content);
Then you should also escape the $string_to_be_replaced using preg_quote beforehand. It's necessary for the - in your search string anyway.
$string_to_be_replaced = preg_quote($string_to_be_replaced, "~");
Would also take care of the [ square ] brackets, btw.
And if you're not doing any assertions or complex matching, str_replace() might be an alternative.

PHP / Regex : match json inside json

Just a quick regex question...hopefully
I have a string that looks something like this:
$string = 'some text [ something {"index":"{"index2":"value2"}"}] [something2 {"here to be":"more specific"}]';
I want to be able to get the value:
{"index":"{"index2":"value2"}"}
But all my attempts at matching (or replacing) keep giving me:
{"index":"{"index2":"value2"}
preg_replace('/\[(.*?)({.*?[^}]})*?\]/is', "", $string);
Here I'm matching the whole square bracket area, but hopefully you can see what I'm trying to do.
The negation of the "do not match }" doesn't seem to be doing anything. Maybe I just need an OR in there or something.
Well, thanks if you have time to answer.
The $string could contain multiple instances of the {} so a greedy regex won't work....that I know of.
You can't make a regex count the opening brackets and the corresponding closeing brackets, you should use a simple for loop to do that, but you can get the complete string from the first opening bracket to the last closeing one with a greedy expression like: ({.*}). Note that simple string functions are much faster then regular expressions, so you should use those instead.

regex to find text inside first occurrence of tokens "[]"

I'm grabbing a file via file_get_contents($text_file) that has a token in the beginning of the contents in the form...
[widget_my_widget]
The full contents of the file might be...
[widget_my_widget]
Here is the start of the text file. It may have [] brackets inside it,
but only the first occurrence denotes the token.
I'm looking for the regex to grab the text string inside the first occurrence of the brackets []. In this case, I'd want to return widget_my_widget and load it into my variable.
Thanks in advance for your help.
The first captured group in \[(.+?)\] will match the string inside the square brackets.
In PHP you can use it like this:
if (preg_match('/\[(.+?)\]/', file_get_contents($text_file), $group)) {
print $group[1];
}
At first occurance in this string (the file content), ignore the left square bracket, then match as little as possible, but up to (not including) the right square bracket.
I think Staffan's answer is mostly correct, but I have a minor correction and I think you may want to avoid the ^, or the string will have to start with a bracket and you said you just want the first instance. On to the code...
$str = ...
$pattern = '/\[([^\]+])\]/';
$n = preg_match($pattern, $str, $matches);
if ($n > 0) {
$first_match = $matches[1];
/// do stuff
}
My pattern looks a little confusing because brackets have special meaning, so I'll try to explain it... we're looking for a open bracket, then one or more characters that is not a closing bracket (in this context, the caret means "not"), then a closing bracket. The parenthesis are around the content we want to capture and the inner brackets are a character class. If that makes no sense, just ignore everything I just said.

Categories