i used bellow code to search and find if http is includes in $url address user enters
if (!preg_match("/http:///", $user_website)
but i got this error
Warning: preg_match() [function.preg-match]: Unknown modifier '/' in
i know its becuase of // of http but how work arround this !?
Instead of having to escape every / in URL regexes it's often useful to use preg_* alternative characters to mark the start/end of the pattern.
if (!preg_match("#http://#", $user_website)
The delimiter you are using / is found in the pattern as well. In such cases you can either escape the delimiter in the pattern:
if (!preg_match("/http:\/\//", $user_website)
or you can choose a different delimiter. This will keep the pattern clean and short:
if (!preg_match("#http://#", $user_website)
You can escape the slashes like the other answers mention, or alternatively you can use different delimiters, preferably characters you won't use in your regex:
preg_match('~http://~', ...)
preg_match('!http://!', ...)
And you don't really need regex for this. String matching should be enough:
if (strpos($user_website, 'http://') !== false) {
// do something
}
See: strpos()
Surely you must do
$parts = parse_url($my_url);
$parts['scheme'] will then contain the url scheme (might be 'http').
Escape / characters with \ characters.
You need to escape literal characters. Place a back-slash before your forward slashes.
http:// becomes http:\/\/
if (!preg_match("/http:\/\//", $user_website)
Related
I want to see if the current request is on the localhost or not. For doing this, I use the following regular expression:
return ( preg_match("/(^localhost).*/", $url) == true ||
preg_match("/^({http|ftp|https}://localhost).*/", $url) == true )
? true : false;
And here is the var_dump() of $url:
string 'http://localhost/aone/public/' (length=29)
Which keeps returning false though. What is the problem of this regular expression?
You are currently using the forward slash (/) as the delimiter, but you aren't escaping it inside your pattern string. This will result in an error and will cause your preg_match() statement to not work (if you don't have error reporting enabled).
Also, you are using alternation incorrectly. If you want to match either foo or bar, you'd write (foo|bar), and not {foo|bar}.
The updated preg_match() should look like:
preg_match("/^(http|ftp|https):\/\/localhost.*/", $url)
Or with a different delimiter (so you don't have to escape all the / characters):
preg_match("#^(http|ftp|https)://localhost.*#", $url)
Curly braces have a special meaning in a regex, they are used to quantify the preceding character(s).
So:
/^({http|ftp|https}://localhost).*/
Should probably be something like:
#^((http|ftp|https)://localhost).*#
Edit: changed the delimiters so that the forward slash does not need to be escaped
This
{http|ftp|https}
is wrong.
I suppose you mean
(http|ftp|https)
Also, if you want only group and don't capture, please add ?::
(?:http|ftp|https)
I would change your current code to:
return preg_match("~^(?:(?:https?|ftp)://)?localhost~", $url);
You were using { and } for grouping, when those are used for quantifying and otherwise mean literal { and `} characters.
A couple of things to add is that:
you can use https? instead of (http|https);
you can use other delimiters for the regex when your pattern has those symbols as delimiters. This avoids you excessive escaping;
you can combine the two regex, since one part is optional (the (?:https?|ftp):// part) and doing so would make the later comparator unnecessary;
the .* at the end is not required.
I have made a regular expression to remove a script tag from a imported page.(used curl)
<script[\s\S]*?/script> this is my expresion
when i used it with preg_replace to remove the tag it gave me this error
Warning: preg_replace() [function.preg-replace]: Unknown modifier 'c' in C:\xampp\htdocs\get_page.php on line 21
can anyone help me
thanks
You should choose a suitable delimiter for your regular expression (preferably one that doesn't' occur anywhere in your pattern, so that you don't need to escape). For example:
"#<script[\s\S]*?/script>#"
Also, don't do that if you are trying to prevent malicious people from injecting Javascript into your page. It can easily be worked around. Use a whitelist of known safe constructs rather than trying to remove dangerous code.
PHP requires delimiters on RegExp patterns. Also, your expression can be simplified.
|<script.+/script>|
Did you wrap your regexp in forward slashes?
$str = preg_replace('/<script[\s\S]*?\/script>/', ...);
Did you surround your regular expression with a delimiter, such as /? If you didn't, you need to. If you did, and you used / (as opposed to your other choices) you'll need to escape the / in your /script, so it'll look like \/script instead.
Use the following code :
$result = preg_replace('%<script[\s\S]*?/script>%', $change_to, $subject);
I am trying to match a Youtube URL with regex to see if it is valid. This is my code:
if(preg_match('\bhttp://youtube.com/watch\?v=.*\b', $link))
{
echo "matched youtube";
}
But I'm getting an error:
Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in C:\xampp\htdocs\ajax\youtube.php on line 22
I'll admit I am a complete novice to regular expressions and I don't understand them much but I am trying to learn as I do this. I made the above regex using this online regex tool:
http://gskinner.com/RegExr/
and it works there. So what am I doing wrong and is there a better way to validate a youtube URL?
Thanks. :)
There's really no need for preg_match here:
$url = "http://youtube.com/watch?v=abc";
if(strpos($url, "http://youtube.com/watch?v=") === 0) {
echo "Valid";
}
PCRE require delimiters that separate the regular expressions and optional modifiers.
In this case the \ is assumed but \ is not a valid delimiter (see error message). Use a different character like ~:
preg_match('~\bhttp://youtube\.com/watch\?v=.*\b~', $link)
You should add addition delimeters to your regexp. This is used to supply optional parameters:
preg_match('"\bhttp://youtube.com/watch\?v=.*\b"', $link)
Symbol / is usually used as regexp delimeter, but in your case it'll force inner / to be escaped. So for more clear view I suggest to use ".
When using preg_match, then the regexp needs to be enclosed with proper delimiters.
For example:
preg_match('/\bhttp://youtube.com/watch\?v=.*\b/', $link)
In your example \b stands for word boundary, this is not a valid alphanumeric delimiter, hence the error message
I am using a lib which uses
eregi($match="^http/[0-9]+\\.[0-9]+[ \t]+([0-9]+)[ \t]*(.*)\$",$line,$matches)
but as eregi is deprecated now, i want to convert above to preg_match. I tried it as below
preg_match($match="/^http/[0-9]+\\.[0-9]+[ \t]+([0-9]+)[ \t]*(.*)\$/i",$line,$matches)
but it throws an error saying Unknown modifier '[' in filename.php
any ideas how to resolve this issue?
Thanks
If you use / as the regex delimiter (ie. preg_match('/.../i', ...)), you need to escape any instances of / in your pattern or php will think it's referring to the end of the pattern.
You can also use a different character such as % as your delimiter:
preg_match('%^http/[0-9]+\.[0-9]+[ \t]+([0-9]+)[ \t]*(.*)$%i',$line,$matches)
You need to escape the delimiters inside the regular expression (in this case the /):
"/^http\\/[0-9]+\\.[0-9]+[ \t]+([0-9]+)[ \t]*(.*)\$/i"
But you could also chose a different delimiter like ~:
"~^http/[0-9]+\\.[0-9]+[ \t]+([0-9]+)[ \t]*(.*)\$~i"
You can try:
preg_match("#^http/[0-9]+\\.[0-9]+[ \t]+([0-9]+)[ \t]*(.*)\$#i",$line,$matches)
You can drop the the $match=
You are using / as the delimiter
and there is another / present in the
regex after http, which effectively
marks the end of your regex. When PHP
sees the [ after this it complains.
You can use a different set of
delimiters as # or escape the / after http
if (preg_match('(\p{Nd}{4}/\p{Nd}{2}/\p{Nd}{2}/\p{L}+)', '2010/02/14/this-is-something'))
{
// do stuff
}
The above code works. However this one doesn't.
if (preg_match('/\p{Nd}{4}/\p{Nd}{2}/\p{Nd}{2}/\p{L}+/u', '2010/02/14/this-is-something'))
{
// do stuff
}
Maybe someone could shed some light as to why the one below doesn't work. This is the error that is being produced:
A PHP Error was encountered
Severity: Warning
Message: preg_match()
[function.preg-match]: Unknown
modifier '\'
Try this: (delimit the regex with ())
if (preg_match('#\p{Nd}{4}/\p{Nd}{2}/\p{Nd}{2}/\p{L}+#', '2010/02/14/this-is-something'))
{
// do stuff
}
Edited
The modifier u is available from PHP 4.1.0 or greater on Unix and from PHP 4.2.3 on win32.
Also as nvl observed, you are using / as the delimiter and you are not escaping the / present in the regex. So you'lll have to use:
/\p{Nd}{4}\/\p{Nd}{2}\/\p{Nd}{2}\/\p{L}+/u
To avoid this escaping you can use a different set of delimiters like:
#\p{Nd}{4}/\p{Nd}{2}/\p{Nd}{2}/\p{L}+#
or
#\p{Nd}{4}/\p{Nd}{2}/\p{Nd}{2}/\p{L}+#
As a tip, if your delimiter is present in your regex, its better to choose a different delimiter not found in the regex. This keeps the regex clean and short.
In the second regex you're using / as the regex delimiter, but you're also using it in the regex. The compiler is trying to interpret this part as a complete regex:
/\p{Nd}{4}/
It thinks the next character after the second / should be a modifier like 'u' or 'm', but it sees a backslash instead, so it throws that cryptic exception.
In the first regex you're using parentheses as regex delimiters; if you wanted to add the u modifier, you would put it after the closing paren:
'(\p{Nd}{4}/\p{Nd}{2}/\p{Nd}{2}/\p{L}+)u'
Although it's legal to use parentheses or other bracketing characters ({}, [], <>) as regex delimiters, it's not a good idea IMO. Most people prefer to use one of the less common punctuation characters. For example:
'~\p{Nd}{4}/\p{Nd}{2}/\p{Nd}{2}/\p{L}+~u'
'%\p{Nd}{4}/\p{Nd}{2}/\p{Nd}{2}/\p{L}+%u'
Of course, you could also escape the slashes in the regex with backslashes, but why bother?