PHP Regex add certain characters - php

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.

Related

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

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.

Terrible with regex

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.

How to add more characters to preg replace array

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

Categories