PHP - How to replace eregi code by preg match [duplicate] - php

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 9 years ago.
Hi I have the following code that that uses the eregi() function that PHP is saying is depreciated. I am trying to convert it into a replacement function (preg match) but im not too sure how to go about it as I am new to preg match function.
eregi ('^[[:alnum:]][a-z0-9_\.\-]*#[a-z0-9\.\-]+\.[a-z]{2,4}$', stripslashes(trim($_POST['email'])))
How can I layout the above code in the preg match function?
Thanks

filter_var(stripslashes(trim($_POST['email'])), FILTER_VALIDATE_EMAIL)
will do the task you are trying to complete, and it validates email addresses even better ;)
Otherwise use preg_match and replace [:alnum:] with [0-9A-Za-z] in the pattern.
See
filter_var
List of validate filters

Related

Regex PHP part of string [duplicate]

This question already has answers here:
Variable-length lookbehind-assertion alternatives for regular expressions
(5 answers)
Closed 4 years ago.
I cant get my regexpression to work in php. It works in javascript (vuejs):
(?<=.+: )(.*)
I have this string:
NL: abcdef
and i would like to get
abcdef
Can someone please tell me what i am doing wrong?
There are many ways to solve this using PHP/PCRE, one is to skip the preceding string using \K
[^:]+: \K(.*)
Regex Demo
If you can add an anchor to the beginning of the string, even better: ^[^:]+: \K(.*)

Deprecated: Function ereg() is deprecated in ...(Google cooridinates map) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I convert ereg expressions to preg in PHP?
I am getting the above (subject) error from:
if (ereg('<coordinates>([0-9.-]{1,}),([0-9.-]{1,}).*</coordinates>', $result, $regs))
So I did this:
if (preg_match('<coordinates>/[0-9.-]{1,}\/,\/[0-9.-]{1,}/.*</coordinates>/', $result, $regs))
Now the Google map doesn't show up at all and it also warns "..cannot find coordinates..."
Where did I go wrong?
Thanks!
You need delimiters around your pattern when using PCRE functions:
if (preg_match('~<coordinates>/[0-9.-]{1,}\/,\/[0-9.-]{1,}/.*</coordinates>/~', $result, $regs))
Notice the ~ at the beginning and end.
Normally, people use / as a delimiter, but because it shows up so frequently in your pattern, an alternate delimiter has been selected.
As #nickb mentioned, have a look at this discussion:
How can I convert ereg expressions to preg in PHP?

How to extract an id from facebook video link with regular expression? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Regex to get value of URL parameter?
If you have a facebook link such as
http://www.facebook.com/photo.php?v=107084586333124
What regular expression would extract the number at the end 107084586333124
edit: I use php
Use PHP's built-in functions to reliably pull query string variables out of a URL. You would use something like:
parse_str(parse_url($url , PHP_URL_QUERY), $v);//where $v is not set yet or an empty array
$v = #$v['v'];//will now contain the value of v or be null if not found
I'm not familiar with the PHP regex engine, but this simple regular expression should do the trick. You will find your number in the first capture group.
v=(\d+)
Replace the following regexes with "".
.*?\?v\=
\D*.*
The remaining data is your number.

Is this regex right (eregi to preg_match conversion) [duplicate]

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 3 years ago.
I am replacing PHP deprecated functions in my website. I have this code:
(eregi("<[^>]*object.*\"?[^>]*>", $secvalue))
It is written on the php.net site that it eregi should be replaced with preg_match with i modifier.
Is this coded right?
(preg_match("<[^>]*object.*\"?[^>]*/i>", $secvalue))
or should I place /i somewhere else?
preg_match('/<[^>]*object.*\"?[^>]*>/i', $secvalue)
You need to add a forward slash at the beginning to match the closing one:
preg_match('/<[^>]object."?[^>]*>/i', $secvalue);

eregi > preg_match convert [duplicate]

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 3 years ago.
I need this php code changed to preg_match please
if(eregi("someurl",$data))
if (preg_match('~someurl~i', $data)). Delimiters can be any single character — I've used ~ instead of / used in examples on PHP site, because if you're putting URL in there, you'd have to escape /. i after the second delimiter is a flag triggering case-insensitivity — PCRE doesn't have separate functions for that.

Categories