PHP Use Regex to match whitespace as well as empty characters - php

I'll start off with some code, this is my current setup:
$search = [
'/\{\{\sSTRING\s\}\}/is',
'/\{\{STRING\}\}/is'
];
$replace = [
'<b>TEST_STRING</b>',
'<b>TEST_STRING</b>'
];
echo preg_replace( $search, $replace, "{{STRING}}" );
This would then output TEST_STRING, as wanted, but I'm using two REGEX statements to make this work, and I want this to work with strings like {{ STRING }} as well as {{STRING}} by only using a single REGEX.
I thought the statement would also ignore there being whitespace, but the \s statement specifically looks for anything relating to [\r\n\t\f\v ], is there an expression to use that will ignore whitespace as well as include it?

You can add an optional quantifier "?" after whitespace character "\s".
$search = '/\{\{\s?STRING\s?\}\}/is';
$replace = '<b>TEST_STRING</b>';
echo preg_replace( $search, $replace, "{{STRING}}" );
Hope this helps.

Related

Normalizing html lost space inside between two tags

i use this function for normalize html:
function normalize_html( $html ) {
return preg_replace(
array(
"/\>[^\S]+/s", "/[^\S]+\</s", "/(\s)+/s", "/\<[^\s\S]+?\>/",
),
array(
">", "<", "\\1", "",
), $html );
}
When i have something like:
<p>text: <b>bold</b></p>
It return: text:bold losting space between text: and bold.
How i can fix this problem into function for not lost this space?
Thanks.
Using regex and html is usually not a good mix, but as this is about the whitespaces and brackets you might give this a go.
The second pattern "/[^\S]+\</s" is the same as \s+"/</s" which matches 1+ whitespace chars before the <. You can update that to "/</s"
Note that this pattern [^\s\S] will not match anything, as it means do not match any char due to the negation using [^, so you can omit the last part.
You don't have to escape the < and the > and you can omit /s after the pattern as it makes the dot to match a newline but there is no dot in the pattern.
function normalize_html( $html ) {
return preg_replace(
array(
"/>\s+/", "/</", "/(\s)+/"
),
array(
">", "<", "\\1", "",
), $html );
}
echo normalize_html("<p>text: <b>bold</b></p>");
Output
<p>text: <b>bold</b></p>
Php demo
If you want to take out HTML tags, you can just use strip_tags:
echo strip_tags('<p>text: <b>bold</b></p>'); // OUTPUT: "text: bold"

How to remove leading characters and the query character with a regular expression in PHP?

I am using preg_replace() and a regular expression to remove all characters before a hyphen (-). I'd like to update the expression to also remove the hyphen itself. The full line of code is shown below in context.
$item['options']['Size'] = preg_replace('/^[^-]*/', '', $item['options']['Size']);
So as it stands let's say I have the below string:
TEST123-150X200
The current preg_replace function will leave me with:
-150X200
I'd like to end up with:
150X200
Could anyone suggest how I can update the regular_expression to achieve this. Thanks
You can add a hyphen at the end of the pattern.
$item['options']['Size'] = preg_replace('/^[^-]*-/', '', $item['options']['Size']);
^
This way, the hyphen is matched (=consumed) and will be removed. Note that [^-] is a negated character class that matches any character but a -. Thus the hyphen was not matched by your original regex.
A non-regex approach:
$item['options']['Size'] = ltrim(strstr($item['options']['Size'], '-'),'-');
See IDEONE demo
<?php
$item = 'TEST123-150X200'; // string here
echo preg_replace('/^[^-]*-/', '', $item);
?>
In addition to the answers/comments given, you could also use a positive lookbehind and replace this:
<?php
$str = "TEST123-150X200";
$regex = '/.*(?<=-)/i';
$item['options']['Size'] = preg_replace($regex, '', $str);
// output: 150X200
?>
Alternatively (as described in the comment), start counting from 1:
$item['options']['Size'] = substr(preg_replace('/^[^-]*/', '', $item['options']['Size']), 1);
I dont think it needs a regex for this...
$str = "TEST123-150X200";
var_dump(end(explode("-", $str))); //string(7) "150X200"
var_dump(ltrim(strstr($str, "-"), "-"));//string(7) "150X200"
var_dump(substr(strrchr($str, "-"), 1) );//string(7) "150X200"

remove double square brackets and keep the string

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

PHP Regex replace tags

I have a piece of text that contains:
[shipping_address]
<p><b>#shipping_title#</b></p>
<p>#shipping_name#<br>
#shipping_streetNrBox#<br>
#shipping_zipcode# #shipping_city#<br>
#shipping_country#<br>
</p>
[/shipping_address]
In php if a certain if statements return true, I want to remove the entire block (including [shipping_address][/shipping_address]). I am using a preg_replace but I need some help with the code.
$content = preg_replace("\[shipping_address\](.*?)\[/shipping_address\]", "" , $content);
does not do the trick, can someone help me out please.
This will do the stuff:
$sData = preg_replace('/\[shipping_address\](.*?)\[\/shipping_address\]/si', '', $sData);
-be aware about using pattern delimiters and multiline replacement (s modifier - in this case, it refers to . (dot) symbol). I've also added i modifier to make replacement case-insensitive.
You should use Pattern Modifiers.
s (PCRE_DOTALL):
If this modifier is set, a dot metacharacter in the pattern matches all characters, including newlines. Without it, newlines are excluded. This modifier is equivalent to Perl's /s modifier. A negative class such as [^a] always matches a newline character, independent of the setting of this modifier.
<?php
$string = '123 [shipping_address]
<p><b>#shipping_title#</b></p>
<p>#shipping_name#<br>
#shipping_streetNrBox#<br>
#shipping_zipcode# #shipping_city#<br>
#shipping_country#<br>
</p>
[/shipping_address] test';
var_dump( preg_replace('/\[shipping_address\].*\[\/shipping_address\]/s', '', $string ));
You can try this
preg_replace('/([shipping_address[^>]*])(.*?)([\/shipping_address])/i', '', $string);
If you want to remove the shippingaddress too: then try this
preg_replace('/[shipping_address[^>]*].*?[\/shipping_address]/i', '', $string);
It should work for you:
$content = preg_replace("/\[shipping_address\](.*[\n\r].*)*\[/shipping_address\]/", "" , $content);
You can try this:
$search = "/\[shipping_address\](.*?)\[\/shipping_address]/s";
$replace = " ";
$string = "[shipping_address]
<p><b>#shipping_title#</b></p>
<p>#shipping_name#<br>
#shipping_streetNrBox#<br>
#shipping_zipcode# #shipping_city#<br>
#shipping_country#<br>
</p>
[/shipping_address]";
echo preg_replace($search,$replace,$string);

How to remove a string between the specific characters using regular expression in PHP?

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.

Categories