This question already has an answer here:
"Unknown modifier 'g' in..." when using preg_match in PHP?
(1 answer)
Closed 7 years ago.
I have a script I wrote to scan several websites for a google link to make sure it is there. For some reason my script is not working. When I check it at http://www.regexr.com/, it works, but not in live implementation.
example of a link its supposed to find:
https://plus.google.com/+VincentsHeatingPlumbingIncPortHuronTownship/about?hl=en
preg_match I am using:
if (preg_match_all("/(.*)plus.google.com(.*)/", $attributeValue->value))
{
$googleLinkCount ++;
$googleLinkHrefs[] = $attributeValue->value;
}
Don't use a regular expression, use parse_url:
if (parse_url($attributeValue->value, PHP_URL_HOST) === 'plus.google.com') {
// host is plus.google.com
}
Related
This question already has answers here:
PHP: Best way to check if input is a valid number?
(6 answers)
Closed 2 years ago.
So I have been trying to bring back to life my very old website.
I started with replacing ereg with preg, but it's been a very long time since I have written any PHP.
At the moment I am stuck on this:
$_POST['amount'] = preg_replace("/[^0-9/]",'',$_POST['amount']);
$_POST['amount'] = round($_POST['amount']);
if (!preg_match('/[^0-9]/', $_POST['amount'])) {
echo "Invalid amount.";
}else {
echo "Passed";
}
I'm not entirely sure where I am going wrong. Should it be !preg_match or preg_match for example?
Edit:
$_POST['amount'] allows a user to enter a number and needs to replace anything else other than a number if attempted.
Not sure what the error is (i.e. what is the current output and what was expected). However, one mistake in the prompt is that the pattern for the first preg_replace appears to have a typo: /[^0-9/] should probably be /[^0-9]/
preg_replace("/[^0-9]/",'',$_POST['amount']);
The rest looks like correct syntax (i.e. preg_match returns 0 if there is no match).
This question already has answers here:
PHP detect (or remove) current drive letter?
(2 answers)
Closed 5 years ago.
I'm trying to get the drive letter from a path in PHP windows.
I'm in this directory:
c:\Program Files\Adobe\
and I want to return:
c
What's the best way to do this? Is there an alternative to parse_url(), but for local paths (and usable under Windows?).
Use substr function from php.
$dir = 'c:/...';
$letter = substr($dir,0,1);
You could also use explode in php, which actually works similar to split.
$drive = explode(":",$path)[0];
This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 3 years ago.
I know there are a lot of questions related to this subject, but after some days of research I didn't find something that could help this particular case, so here it is:
I have replaced the deprecated eregi with preg_match in 2 files of my website, and now the capcha code gives an error on the registration page, even if the code is absolutely correct
On the registration page I have replaced this
function is_valid_username($username) {
if(!eregi("^[a-z0-9]*$", trim(str_replace(" ","",$username)))) {
return 0;
}
with this
if(!preg_match("^[a-z0-9]*$^", trim(str_replace(" ","",$username)))) {
return 0;
}
And in my second file I have replaced this:
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) {
$result = 0;
}
with this
if(!preg_match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$^", $email)) {
$result = 0;
}
How can I resolve this issue?
eregi is case-insensitive, so you would need to add the i modifier to the end of your preg_match expression.
Also, ^ denotes the start of the input and you have used it as the delimiter.
So this should be more like the original:
#^[a-z0-9]*$#i
and
#^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$#i
By the way, I don't know what your captcha code requires exactly, but there are easier ways to verify an email address using filter_var().
This question already has answers here:
Beautiful way to remove GET-variables with PHP?
(12 answers)
How to remove content from url after question mark. preg_match or preg_replace?
(2 answers)
Closed 8 years ago.
I have this url: http:www.blabla.com/x/x/x/x?username=testuser
I need a string to read this url, but forget everything and including the ? mark.
So it becomes this: http:www.blabla.com/x/x/x/x
The reason for this is because I am making this variable:
$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
And this code:
if($host == "http:www.blabla.com/x/x/x/x") {
echo "lul";
}
But right now, the URL changes depending on what user is on, and it has to execute the echo no matter what user is on.
So I read some reges and preg_match etc. and I just wanted to hear your opinions or advice. How would I accomblish this the best? thanks!
This is too trivial of a task for regex.
$host = $_SERVER['SERVER_NAME'] . explode("?", $_SERVER['REQUEST_URI'], 2)[0];
(Note: this assumes you're up-to-date, or at least using PHP 5.4, for the dereference to work without a temporary variable)
Or if you must omit the get / request section just explode ? and use $host[0]
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP startsWith() and endsWith() functions
Check if variable starts with ‘http’
How make an if-statement that if $mystring has the prefix "http://" then {do something}.
I've done this in objective-c like this:
if([mynsstring hasPrefix:#"http://"])
{
//Do something...
}
I don't know how to do this in PHP.
Thanks for your help in advance.
Simplest would be using substring to compare
if (substr($mystring, 0, 7) === 'http://') {
// do something
}
Remember of course to take the exact number of characters.