I need to remove all square brackets from a string and keep the string. I've been looking around but all topic OP's want to replace the string with something.
So: [[link_to_page]]
should become: link_to_page
I think I should use php regex, can someone assist me?
Thanks in advance
You can simply use a str_replace.
$string = str_replace(array('[[',']]'),'',$string);
But this would get a '[[' without a ']]' closure. And a ']]' without a '[[' opening.
It's not entirely clear what you want - but...
If you simply want to "remove all square brackets" without worrying about pairing/etc then a simple str_replace will do it:
str_replace( array('[',']') , '' , $string )
That is not (and doesn't need to be) a regex.
If you want to unwrap paired double brackets, with unknown contents, then a regex replace is what you want, which uses preg_replace instead.
Since [ and ] are metacharacters in regex, they need to be escaped with a backslash.
To match all instances of double-bracketed text, you can use the pattern \[\[\w+\[\] and to replace those brackets you can put the contents into a capture group (by surrounding with parentheses) and replace all instances like so:
$output = preg_replace( '/\[\[(\w+)\[\]/' , '$1' , $string );
The \w matches any alphanumeric or underscore - if you want to allow more/less characters it can be updated, e.g. \[\[([a-z\-_]+)\[\] or whatever makes sense.
If you want to act on the contents of the square brackets, see the answer by fluminis.
You can use preg_replace:
$repl = preg_replace('/(\[|\]){2}/', '', '[[link_to_page]]');
OR using str_replace:
$repl = str_replace(array('[[', ']]'), '', '[[link_to_page]]');
If you want only one match :
preg_match('/\[\[([^\]]+)\]\]/', $yourText, $matches);
echo $matches[1]; // will echo link_to_page
Or if you want to extract all the link from a text
preg_match_all('/\[\[([^\]]+)\]\]/', $yourText, $matches);
foreach($matches as $link) {
echo $link[1];
}
How to read '/\[\[([^\]]+)\]\]/'
/ start the regex
\[\[ two [ characters but need to escape them because [ is a meta caracter
([^\]]+) get all chars that are not a ]
\]\] two ] characters but need to escape them because ] is a meta caracter
/ end the regex
Try
preg_replace(/(\[\[)|(\]\])/, '', $string);
Related
I wan to get he text between the HTML comments start and end Like
<!--Q1-->
\nフレンチブルドックと遊んでるとき\n
<!--Q1END-->\n
<!--Q2-->
\n表参道、新宿、銀座\n
<!--Q2END-->\n
<!--Q3-->
\nヒューマンドラマ全般が好きです。<BR>\n<BR>\n好きなアーティスト サザンオールスターズ\n
<!--Q3END-->
I want to get it as array like this
$data = [
1 => 'フレンチブルドックと遊んでるとき',
2 => '表参道、新宿、銀座',
3 = 'ヒューマンドラマ全般が好きです。<BR>\n<BR>\n好きなアーティスト サザンオールスター ズ'
]
So how can i find the text between html comments ?
Thanks in advance
Here's a regex that would get you what you want for the above string:
/<!--Q(\d)-->\n\\n(.*)\\n\n<!--Q\1END-->/gs
(Note: This removes the literal '\n' before and after each of the strings you want since this is what you have above, but if the strings don't have this, it won't match either.)
To put that into PHP remember you have to double escape the literal backslashes. Unfortunately it's quite ugly to keep track of all the newlines and literal '\n' strings (at least to me).
preg_match_all('/<!--Q(\d)-->\n\\\\n(.*)\\\\n\n<!--Q\1END-->/s', $text, $matches);
print_r($matches[2]);
Or if you want something more readable, you can remove the literal '\n' strings from the input text, match everything between the HTML quotes and then trim it:
// Remove all literal '\n' strings from the text
$text = preg_replace('#\\\\n#', '', $text);
// Match desired strings
preg_match_all('/<!--Q(\d)-->(.*)<!--Q\1END-->/s', $text, $matches);
// Trim all desired strings
$output = array_map('trim', $matches[2]);
To get literally what you want lookarounds are good option:
(?<=<!--([A-Z]\d)-->)[\s\S]*?(?=<!--\1END-->)
Demo
Caveat: Works as long as your comment keys (e.g. Q1) do not exceed A0-Z9. You cannot simply use [A-Z]\d+ instead since PHP's/PCRE regex engine does not like quantifiers/variable length patterns in lookbehinds.
Otherwise, I recommend using a capture group like this:
<!--([A-Z]\d+)-->([\s\S]*?)<!--\1END-->
Use it in your code like this:
$re = '/<!--([A-Z]\d+)-->([\s\S]*?)<!--\1END-->/s';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
To get rid of the newline, just use trim(), there are several ways to apply it, e.g. a foreach, a map, etc.
foreach ($matches as $match){
$result[] = trim($match[2]);
}
var_dump($result);
I'm trying to split a string at question marks, exclamation marks, or periods, but at the same time I'm trying to keep the punctuation marks after splitting them. How would I do that? Thanks.
$input = "Sentence1?Sentence2.Sentence3!";
$input = preg_split("/(\?|\.|!)/", $input);
echo $input[0]."<br>";
echo $input[1]."<br>";
echo $input[2]."<br>";
Desired outputs:
Sentence1?
Sentence2.
Sentence3!
Actual outputs:
Sentence1
Sentence2
Sentence3
You can do this by changing the capture group in your regex into a lookbehind like so:
$input = preg_split("/(?<=\?|\.|!)/", $input);
the manual knows all
PREG_SPLIT_DELIM_CAPTURE
If this flag is set, parenthesized expression in the delimiter pattern will be captured and returned as well.
so in your case:
$input = preg_split("/(\?|\.|!)/", $input,NULL,PREG_SPLIT_DELIM_CAPTURE);
I went through dozen of already answered Q without finding one that can help me.
I have a string like this:
aaa.{foo}-{bar} dftgyh {foo-bar}{bar} .? {.!} -! a}aaa{
and I want to obtain a string like this:
aaa{foo}-{bar}dftgyh{foo-bar}{bar}-aaaa
Essentially I want to keep:
valid word chars and hyphens wrapped in an open and a closed curly bracket, something that will match the regex \{[\w\-]+\}
all the valid word chars and hyphens outside curly brackets
Using this:
$result = preg_replace( array( "#\{[\w\-]+\}#", '#[\w\-]#' ), "", $string );
I obtain the exact contrary of what I want: I remove the part that I want to keep.
Sure I can use ^ inside the square brackets in the second pattern, but it will not work for the first.
I.e. this will not work (the second pattern in the array is valid, the first not):
$result = preg_replace( array( "#[^\{[\w\-]+\}]#", '#[^\w\-]#' ), "", $string );
So, whis the regex that allow me to obtain the wanted result?
You may consider matching what you want instead of replacing the characters you do not want. The following will match word characters and hyphen both inside and outside of curly braces.
$str = 'aaa.{foo}-{bar} dftgyh {foo-bar}{bar} .? {.!} -! a}aaa{';
preg_match_all('/{[\w-]+}|[\w-]+/', $str, $matches);
echo implode('', $matches[0]);
Output as expected:
aaa{foo}-{bar}dftgyh{foo-bar}{bar}-aaaa
Also an option to (*SKIP)(*F) the good stuff and do a preg_replace() with the remaining:
$str = preg_replace('~(?:{[-\w]+}|[-\w]+)(*SKIP)(*F)|.~' , "", $str);
test at regex101; eval.in
I have string like below,
$string = "test coontevt [gallery include=\"12,24\"] first [gallery include=\"12,24\"] second";
i need to remove the string starts with [gallery to first ocuurance of it's ].
i already use this one,
$string12 = preg_replace('/[gallery.+?)+(/])/i', '', $string);
but i get empty string only.
Finally i want result for the above string is,
$string ="test coontevt first second".
How can i do this using regular expression?.
plz help me?
The character [ is a regex meta-character. TO match a literal [ you need to escape it.
$string12 = preg_replace('/\[gallery.+?\]/i', '', $string);
or
$string12 = preg_replace('/\[gallery[^\]]+\]/i', '', $string);
You need to escape the square brackets
$string12 = preg_replace('/\[gallery.+?\]/i', '', $string);
The round brackets are unnecessary so I removed them, also the quantifier between those brackets and the forward slash before the last square bracket.
To avoid multiple space in the result, I would match also the surrounding spaces and replace with 1 space.
\s+\[gallery.+?\]\s+ and replace with one space
$string12 = preg_replace('/\s+\[gallery.+?\]\s+/i', ' ', $string);
See this expression here online on Regexr
Try it like this:
$string12 = preg_replace('/\[gallery[^\]]+\]/i', '', $string);
[^\]]+ means that there can be one or more character that is not ]. And there is no need for any ( and ) if you don't want to use the backreferences.
I'm trying to use regular expressions (preg_match and preg_replace) to do the following:
Find a string like this:
{%title=append me to the title%}
Then extract out the title part and the append me to the title part. Which I can then use to perform a str_replace(), etc.
Given that I'm terrible at regular expressions, my code is failing...
preg_match('/\{\%title\=(\w+.)\%\}/', $string, $matches);
What pattern do I need? :/
I think it's because the \w operator doesn't match spaces. Because everything after the equal sign is required to fit in before your closing %, it all has to match whatever is inside those brackets (or else the entire expression fails to match).
This bit of code worked for me:
$str = '{%title=append me to the title%}';
preg_match('/{%title=([\w ]+)%}/', $str, $matches);
print_r($matches);
//gives:
//Array ([0] => {%title=append me to the title%} [1] => append me to the title )
Note that the use of the + (one or more) means that an empty expression, ie. {%title=%} won't match. Depending on what you expect for white space, you might want to use the \s after the \w character class instead of an actual space character. \s will match tabs, newlines, etc.
You can try:
$str = '{%title=append me to the title%}';
// capture the thing between % and = as title
// and between = and % as the other part.
if(preg_match('#{%(\w+)\s*=\s*(.*?)%}#',$str,$matches)) {
$title = $matches[1]; // extract the title.
$append = $matches[2]; // extract the appending part.
}
// find these.
$find = array("/$append/","/$title/");
// replace the found things with these.
$replace = array('IS GOOD','TITLE');
// use preg_replace for replacement.
$str = preg_replace($find,$replace,$str);
var_dump($str);
Output:
string(17) "{%TITLE=IS GOOD%}"
Note:
In your regex: /\{\%title\=(\w+.)\%\}/
There is no need to escape % as its
not a meta char.
There is no need to escape { and }.
These are meta char but only when
used as a quantifier in the form of
{min,max} or {,max} or {min,}
or {num}. So in your case they are treated literally.
Try this:
preg_match('/(title)\=(.*?)([%}])/s', $string, $matches);
The match[1] has your title and match[2] has the other part.