I cant figure out preg_replace at all, it just looks chinese to me, anyway I just need to remove "&page-X" from a string if its there.
X being a number of course, if anyone has a link to a useful preg_replace tutorial for beginners that would also be handy!
Actually the basic syntax for regular expressions, as supported by preg_replace and friends, is pretty easy to learn. Think of it as a string describing a pattern with certain characters having special meaning.
In your very simple case, a possible pattern is:
&page-\d+
With \d meaning a digit (numeric characters 0-9) and + meaning: Repeat the expression right before + (here: \d) one or more times. All other characters just represent themselves.
Therefore, the pattern above matches any of the following strings:
&page-0
&page-665
&page-1234567890
Since the preg functions use a Perl-compatible syntax and regular expressions are denoted between slashes (/) in Perl, you have to surround the pattern in slashes:
$after = preg_replace('/&page-\d+/', '', $before);
Actually, you can use other characters as well:
$after = preg_replace('#&page-\d+#', '', $before);
For a full reference of supported syntax, see the PHP manual.
preg_replace uses Perl-Compatible Regular Expression for the search pattern. Try this pattern:
preg_replace('/&page-\d+/', '', $str)
See the pattern syntax for more information.
$outputstring = preg_replace('/&page-\d+/', "", $inputstring);
preg_replace()
preg_replace('/&page-\d+/', '', $string)
Useful information:
Using Regular Expressions with PHP
http://articles.sitepoint.com/article/regular-expressions-php
Related
I'm using ajax check function to check inserted category name which should be only alpha-numeric and also allowed space
I've used this function eregi_replace with the following regular expression [a-zA-Z0-9_]+
$check = eregi_replace('([a-zA-Z0-9_]+)', "", $catname);
But when i insert category name for example hello world it failed cause it does not accept space but if i write it as helloworld works so i understood that the error must be in the regular expression i'm using.
so what is the correct regular expression that filter any special characters and allow only for alpha-numeric and space.
Thanks a lot
A character class matching letters, numbers, the underscore and space would be
[\w ]
You should not be using any of the POSIX regular expression functions as they are now deprecated. Instead, use their superior counterparts from the PCRE suite.
Change your regular expression to:
([A-Za-z0-9_]+(?: +[A-Za-z0-9_]+)*)
I realize that it is not as straightforward as you might have hoped. Things to note:
The identifier must start with a non-space
If there are spaces, they should be between words and not matched at the end
?: is used to prevent an extra grouping in your expression, but is not required
The + after the space character allows multiple spaces between words. You can enforce a single space by removing it, but in some solutions, it is a better practice to normalize the space internally with a preg_split that matches on " +" (a space with a plus sign) and then use implode(" ", $array). But eh... if you are just validating, this should be fine.
you've got it nearly right, just add \s into your square brackets and "hello world" will pass.
([A-Za-z0-9_\s]+)
I've got some help by old friend and i've tested and works perfect - thank you all for answers and comments it was very helpful to me.
this works perfect
$check = eregi_replace('(^[a-zA-Z0-9 ]*$)', "", $catname);
Alphanumeric and white space regular expression
#Phil
yours works perfect but still will pass underscore ~ thanks
#Michael Hays
I do not know it didn't worked for whitespace , but your comments is very helpful ~ thanks
#kjetilh
I will read more about $preg ~ thanks
#Alastair
Works fine if i've replaced \s with just whitespace ! ~ thanks
eregi functions are deprecated as of php 5.3. Use preg instead.
I have the following string in php:
$string = 'FEDCBA9876543210';
The string can be have 2 or more (I mean more) hexadecimal characters
I wanted to group string by 2 like :
$output_string = 'FE:DC:BA:98:76:54:32:10';
I wanted to use regex for that, I think I saw a way to do like "recursive regex" but I can't remember it.
Any help appreciated :)
If you don't need to check the content, there is no use for regex.
Try this
$outputString = chunk_split($string, 2, ":");
// generates: FE:DC:BA:98:76:54:32:10:
You might need to remove the last ":".
Or this :
$outputString = implode(":", str_split($string, 2));
// generates: FE:DC:BA:98:76:54:32:10
Resources :
www.w3schools.com - chunk_split()
www.w3schools.com - str_split()
www.w3schools.com - implode()
On the same topic :
Split string into equal parts using PHP
Sounds like you want a regex like this:
/([0-9a-f]{2})/${1}:/gi
Which, in PHP is...
<?php
$string = 'FE:DC:BA:98:76:54:32:10';
$pattern = '/([0-9A-F]{2})/gi';
$replacement = '${1}:';
echo preg_replace($pattern, $replacement, $string);
?>
Please note the above code is currently untested.
You can make sure there are two or more hex characters doing this:
if (preg_match('!^\d*[A-F]\d*[A-F][\dA-F]*$!i', $string)) {
...
}
No need for a recursive regex. By the way, recursive regex is a contradiction in terms. As a regular language (which a regex parses) can't be recursive, by definition.
If you want to also group the characters in pairs with colons in between, ignoring the two hex characters for a second, use:
if (preg_match('!^[\dA-F]{2}(?::[A-F][\dA-F]{2})*$!i', $string)) {
...
}
Now if you want to add the condition requiring tow hex characters, use a positive lookahead:
if (preg_match('!^(?=[\d:]*[A-F][\d:]*[A-F])[\dA-F]{2}(?::[A-F][\dA-F]{2})*$!i', $string)) {
...
}
To explain how this works, the first thing it does it that it checks (with a positive lookahead ie (?=...) that you have zero or more digits or colons followed by a hex letter followed by zero or more digits or colons and then a letter. This will ensure there will be two hex letters in the expression.
After the positive lookahead is the original expression that makes sure the string is pairs of hex digits.
Recursive regular expressions are usually not possible. You may use a regular expression recursively on the results of a previous regular expression, but most regular expression grammars will not allow recursivity. This is the main reason why regular expressions are almost always inadequate for parsing stuff like HTML. Anyways, what you need doesn't need any kind of recursivity.
What you want, simply, is to match a group multiple times. This is quite simple:
preg_match_all("/([a-z0-9]{2})+/i", $string, $matches);
This will fill $matches will all occurrences of two hexadecimal digits (in a case-insensitive way). To replace them, use preg_replace:
echo preg_replace("/([a-z0-9]{2})/i", $string, '\1:');
There will probably be one ':' too much at the end, you can strip it with substr:
echo substr(preg_replace("/([a-z0-9]{2})/i", $string, '\1:'), 0, -1);
While it is not horrible practice to use rtrim(chunk_split($string, 2, ':'), ':'), I prefer to use direct techniques that avoid "mopping up" after making modifications.
Code: (Demo)
$string = 'FEDCBA9876543210';
echo preg_replace('~[\dA-F]{2}(?!$)\K~', ':', $string);
Output:
FE:DC:BA:98:76:54:32:10
Don't be intimidated by the regex. The pattern says:
[\dA-F]{2} # match exactly two numeric or A through F characters
(?!$) # that is not located at the end of the string
\K # restart the fullstring match
When I say "restart the fullstring match" I mean "forget the previously matched characters and start matching from this point forward". Because there are no additional characters matched after \K, the pattern effectively delivers the zero-width position where the colon should be inserted. In this way, no original characters are lost in the replacement.
I am developing an application using PHP but I am new to regular expressions, I could not find a solution to my problem. I want to replace all occurences of #word with a link, i have written a preg_match for this:
$text=preg_replace('~#([\p{L}|\p{N}]+)~u', '#$1', $text);
The problem is, this regular expression also matches the html character codes like
'
and gives corrupt output. I need to exclude the words starting with &# but i do not know how to do that using regular expressions.
Thanks for your help.
'~(?<!&)#([\p{L}|\p{N}]+)~u'
That's a negative lookbehind assertion: http://www.php.net/manual/en/regexp.reference.assertions.php
Matches # only if not preceded by &
http://gskinner.com/RegExr/
use this online regular expression constructor. They have explanation for every flag you may want to use.. and you will see highlighted matches in example text.
and yes use [a-zA-Z]
You would need to add a [A-Za-z] rule in your regular expression statement so that it only limits itself to letters and no numbers.
I will edit with an example later on.
Just migrating from PHP 5.2 to 5.3, lot of hard work! Is the following ok, or would you do something differently?
$cleanstring = ereg_replace("[^A-Za-z0-9]^[,]^[.]^[_]^[:]", "", $critvalue);
to
$cleanstring = preg_replace("[^A-Za-z0-9]^[,]^[.]^[_]^[:]", "", $critvalue);
Thanks all
As a follow-up to cletus's answer:
I'm not familiar with the POSIX regex syntax (ereg_*) either, but based on your criteria the following should do what you want:
$cleanstring = preg_replace('#[^a-zA-Z0-9,._:]#', '', $critvalue);
This removes everything except a-z, A-Z, 0-9, and the puncation characters.
I'm not that familiar with the ereg_* functions but your preg version has a couple of problems:
^ means beginning of string so with it in the middle it won't match anything; and
You need to delimit your regular expression;
An example:
$out = preg_replace('![^0-9a-zA-Z]+!', '', $in);
Note I'm using ! to delimit the regex but you could just as easily use /, ~ or whatever. The above removes everything except numbers and letters.
See Pattern Syntax, specifically Delimiters.
I have a string and I need to see if it contains the following "_archived".
I was using the following:
preg_match('(.*)_archived$',$string);
but get:
Warning: preg_match() [function.preg-match]: Unknown modifier '_' in /home/storrec/classes/class.main.php on line 70
I am new to Regular Expressions so this is probably very easy.
Or should I be using something a lot simpler like
strstr($string, "_archived");
Thanks in advance
strstr($string, "_archived");
Is going to be way easier for the problem you describe.
As is often quoted
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. - Jamie Zawinski
strstr is enough in this case, but to solve your problem, you need to add delimiters to your regex. A delimiter is a special character that starts and ends the regex, like so:
preg_match('/_archived/',$string);
The delimiter can be a lot of different characters, but usual choices are /, # and !. From the PHP manual:
Any character can be used for delimiter as long as it's not alphanumeric, backslash (), or the null byte. If the delimiter character has to be used in the expression itself, it needs to be escaped by backslash. Since PHP 4.0.4, you can also use Perl-style (), {}, [], and <> matching delimiters.
Read all about PHP regular expression syntax here.
You can see some examples of valid (and invalid) patterns in the PHP manual here.
You just need some delimiters, e.g. enclose the pattern with /
preg_match('/_archived$/',$string);
Perl regexes let you use any delimiter, which is handy if your regex uses / a lot. I often find myself using braces for example:
preg_match('{_archived$}',$string);
Also, note that you don't need the (.*) bit as you aren't capturing the bit before "_archived", you're just testing to see if the string ends with it (that $ symbol on the end matches the end of the string)
If all you're looking for is if a string contains a string, then by all means use the simple version. But you can also simply do:
preg_match('/_archived/', $string);
Try:
preg_match('/(.*)_archived$/',$string);
If you are only checking if the string exists in $string, strstr should be enough for you though.
strstr() or strpos() would be better for finding something like this, I would think. The error you're getting is due to not having any delimiters around your regular expression. Try using
"/(.*)_archived$/"
or
"#(.*)_archived$#".
... is probably the best way to implement this. You're right that a regex is overkill.
When you have a specific string you're looking for, using regular expressions in a bit of overkill. You'll be fine using one of PHP's standard string search functions here.
The strstr function will work, but conventional PHP Wisdom (read: myth, legend, superstition, the manual) says that using the strpos function will yield better performance. i.e., something like
if(strpos($string, '_archived') !== false) {
}
You'll want to use the true inequality operator here, as strpos returns "0" when it finds a needle at the start of it's haystack. See the manual for more information on this.
As to your problem, PHP's regular expression engine expects you to enclose your regular expression string with a set of delimiters, which can be one of a number of different characters ( "/" or "|" or "(" or "{" or "#", or ...). The PHP engine thinks you want a regular expression of
.*
with a set of pattern modifiers that are
_archived$
So, in the future, when you use a regular expression, try something like
//equivilant
preg_match('/(.*)_archived$/i',$string);
preg_match('{(.*)_archived$}i',$string);
preg_match('#(.*)_archived$#i',$string);
The "/", "#", and "{}" characters are your delimiters. They are not used in the match, they're used to tell the engine "anything in-between these characters is my reg ex. The "i" at the end is a pattern modifiers that says to be case insensitive. It's not necessary, but I include it here so you can see what a pattern modifier looks like, and to help you understand why you need delimiters in the first place.
You don't have to remember about delimiters if you are using T-Regx
pattern('_archived$')->matches($string)