eregi > preg_match convert [duplicate] - php

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.

Related

What is the use of "ereg_replace("\n","\\n",$row[$j])" expression? [duplicate]

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 2 years ago.
I found one PHP script for get a database backup, there use this expression to do something, that script show call to undefined function ereg_replace() this error, if i remove this line script is working fine...
how to replace this function $row[$j] = ereg_replace("\n","\\n",$row[$j]); to that script working finely,
Can anyone assist me..
ereg_replace deprecated and remove from newer versions of php (7 and up).
You will have to update your code to use preg_replace
$row[$j] = preg_replace("#\n#","\\n",$row[$j]);
One of the differences between ereg_replace() and preg_replace() is that the pattern must be enclosed by delimiters: delimiter + pattern + delimiter, in this case we are using # so we don't have to escape / that is the usual that is used.
Valid Delimiters:
/, #, ~, +, %, #, ! , <, >
Reference from php.net
hope it helps.

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(.*)

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

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

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

replacement for ereg_replace [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I convert ereg expressions to preg in PHP?
I have upgraded php and now im getting the ereg_replace deprecated errors.
I have done some searching round web and found that I can use preg instead but not sure how to change this code correctly
$scriptName = ereg_replace(
"^".$_SERVER["DOCUMENT_ROOT"], "",
$_SERVER["SCRIPT_FILENAME"]
);
Replace the e with a p.
Add a delimiter to the beginning and end of that first argument. Traditionally, people use slashes (/), but I like to use ~ as there is less chance of actually using that character in the regular expression.
Just adding delimiters won't work when special characters are included in $_SERVER["DOCUMENT_ROOT"]'s value. You need to escape them as follows:
$scriptName = preg_replace(
"/^".preg_quote($_SERVER["DOCUMENT_ROOT"],"/")."/",
"",
$_SERVER["SCRIPT_FILENAME"]
);

Categories