i am new to php. Can anyone please tell me how to add more characters like comma and double quotes(, ") to this preg_replace array.
$filter = preg_replace('/[^a-z0-9]/is', '', $text);
I guess you mean the character class:
/[^a-z0-9,"]/is
Should be pretty basic:
$filter = preg_replace('/[^a-z0-9," ]/is', '', $text);
For more information on Regular Expressions check out this site.
EDIT
Given the comment, added the space character to not be removed.
.. this can help you ...
http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/
and you need
$new_text = preg_replace('/[^a-z0-9\s]/is', '', $text);
You may use this regular expression to remove comma and double quote but not space
$filter = preg_replace('/[,"]/is', '', $text);
Related
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"
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);
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 have the following regex that allows any letter and number (and I think uppercase too)!
What I need is to allow certain characters such as a - value and only that.
How would I insert that into this?
$reg = preg_replace('/[^a-z0-9 ]/i', '', $reg);
You can add additional characters inside the brackets like this:
$reg = preg_replace('/[^a-z0-9 \-]/i', '', $reg);
If you meant that you want the expression inside the brackets or a minus, you can use this:
$reg = preg_replace('/([^a-z0-9 ]|\-)/i', '', $reg);
Not tested, but I think thats it.
How can I make this regular expression replace spaces as well any non latin alpha numeric character?
preg_replace('/[^a-zA-Z0-9\s]/', '', $title)
Thanks a lot
[^...] matches anything but ....
\s matches spaces.
You don't want it to not match spaces.
Everything looks ok, you just have to assing it to a variable!
$title = preg_replace('/[^a-zA-Z0-9\s]/', '', $title)
I would just do:
<?php
preg_match_all('/[a-zA-Z0-9\s]/', $title, $out);
$ntitle = implode($out,'');
?>
EDIT: Briedis is right though, your regex works fine.