How can this ereg code possibly be updated to preg_match? [duplicate] - php

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 2 years ago.
I have reviewed the answers to this out the wazoo. I've googled this the same.
I have a variable that works really great with php 5.4 ereg but fails miserably with the latest stuff.
ereg($user.$pass, preg_replace('/\s/', '', trim($_POST['user']).trim($_POST['pass'])))
I have tried the following with no joy:
preg_match('/^$user.$pass/',
preg_match('/{$user.$pass}/',
preg_match({$user.$pass},
preg_match('{$user.$pass}',
I don't know what to do. I've also tried rewriting the code to use different input lines, but to no avail.
Does the rest of the line need escaping as well?
Is it possible that this function just doesn't port easily into the latest php versions?
This is the complete line of code. It looks to see if a user and password match the user and password for this page and trims out spaces. (I probably should sanitize it outside of this if statement.) It also checks the SESSIONS status. It may be a bit ugly but it works great in 5.4:
elseif(((!empty($_POST['user']) and !empty($_POST['pass']) and ereg($user.$pass, preg_replace('/\s/', '', trim($_POST['user']).trim($_POST['pass'])))) or (isset($_SESSION['logged']) and ($_SESSION['logged'] === true) and isset($_SESSION['user']) and ($_SESSION['user'] == $user))) and ($_SESSION['attempts'] < 5))
ADDITIONAL: I found a way around the ereg or the preg_match. It works great.

The (main) usage difference between ereg() and preg_match is the delimiters.
So if your code was:
ereg($user.$pass, preg_replace('/\s/', '', trim($_POST['user']).trim($_POST['pass'])));
the new equivalent code should be:
preg_match('/'.$user.$pass.'/', preg_replace('/\s/', '', trim($_POST['user']).trim($_POST['pass'])));
Please note the lack of {, } or ^ in the first parameter of preg_match().

Related

How to convert preg_replace e modifier to pref_replace_callback [duplicate]

This question already has answers here:
Replace preg_replace() e modifier with preg_replace_callback
(3 answers)
Closed 6 years ago.
I use a preg_replace for replacing words in templates
echo preg_replace('~%(\w+)%~e', '$obj->$1', $template);
$obj is an array of searches, and $template is the file I am searching and replacing. So I am replacing keywords like %REPLACE%.
Now I don't really understand how it works, and apparently the e modifier has just been deprecated so I think I have to use the pref_replace_callback instead. We are upgrading to php7 soon and so I must replace this line in my code.
Now I have already looked on stack overflow and found answers to other peoples similar problems, unfortunately the answers don't help with this particular pattern. I don't understand how this works let alone how to get preg_replace_callback working. I have tried reading up on preg_replace, but really I don't understand how it works.
So I how do I change the above code to preg_replace_callback?
PS. I have searched for tutorials on preg_replace_callback, but nothing that explains what I have to do.
This is not a duplicate of that other question. It is a completely different preg_replace. The answer to the other question does not answer my question, and I have not got the know how to work it out, as I am not very good with the preg_replace keyword.
Pretty sure this will work:
echo preg_replace_callback('~%(\w+)%~',
function($m) use($obj) {
return $obj->{$m[1]};
},
$template);

Need to replace deprecated ereg_replace [duplicate]

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 9 years ago.
I am working for a non-profit and i'm not an expert in PHP.
I need to replace the following code:
$status = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", "\\0", $status);
When I attempt to modify it to preg_replace, I get an error every different way I try to exit the code.
This will do the job:
$statut = preg_replace('~[a-z]+://[^<>\s]+[\w/]~i', '$0', $statut);
But if the goal of this replacement is to keep all urls and transform them into links, you must change the pattern a little. And, why not, test them with filter_validate_url

Strip RTF strings with PHP - Regex [duplicate]

This question already has answers here:
Regular Expression for extracting text from an RTF string
(11 answers)
Closed 9 years ago.
A column in the database I work with contains RTF strings, I would like to strip these out using PHP, leaving just the sentence between.
It is a MS SQL database 2005 if I recall correctly.
An example of the kind of strings pulled from the database (need any more let me know, all the rest are similar):
{\rtf1\ansi\ansicpg1252\deff0\deflang2057{\fonttbl{\f0\fnil\fcharset0 Tahoma;}}
\viewkind4\uc1\pard\lang1033\f0\fs17 ASSEMBLE COMPONENTS AS DETAILED ON DRAWING.\lang2057\fs17\par
}
I would like this to be stripped to only return:
ASSEMBLE COMPONENTS AS DETAILED ON DRAWING.
Now, I have successfully managed to strip the characters in ASP.NET for a previous project, however I would like to do so using PHP. Here is the regular expression I used in ASP.NET, which works flawlessly may I add:
"(\{.*\})|}|(\\\S+)"
However when I try to use the same expression in PHP with a preg_replace it does not strip half of the characters.
Any regex gurus out there?
Use this code. it will work fine.
$string = preg_replace("/(\{.*\})|}|(\\\S+)/", "", $string);
Note that I added a '/' in the beginning and at the end '/' in the regex.

Another eregi replacement issue, for web page navigation [duplicate]

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 3 years ago.
My hosts recently updated their PHP to 5.3 (without warning) and I now have to replace the code for my page navigation on my index page. This is what I'm currently using:
<? if (eregi(".shtml", $load)) {if (!#readfile("$load")) { readfile("error.shtml"); } } if (!eregi(".shtml", $load)) {if (!#readfile(include("/home/content/j/p/l/jplegacy/html/coranto/news.txt") )) ;}?>
This is what I use as a sample in my links to navigate with:
Archive
About Us
I looked at two different techniques, preg_match() and stristr().
preg_match() outputs my error page and news.txt file from Coranto, but doesn't navigate me to the pages like in the links above. What can I do here to make that work?
stristr() doesn't give me the warnings, but it doesn't navigate the page and only outputs my Coranto news page. If this would be better, what can I do to make this work?
What do I need to do to fix this? I am completely lost. :(
ereg(i) is considered to be deprecated. You should use preg_match for regular expressions as the preg is faster than the other engine. Also, if you want ends with ".shtml" that regex is wrong, and you don't need to resort to regular expressions. Ends with .shtml is "^*.shtml$". If you do choose to use preg_match, all preg_match regular expresions, like perl, are surrounded by "/" (slash), so the correct regex would look like:
preg_match("/^.*\.shtml$/", $load)
But on to the better solution for "ends with .shtml". Generally you shouldn't use regular expressions unless the situation actually calls for them (they're significantly slower than using substr and matching the last part of the string), simple matches like that don't qualify.
if (substr($test, sizeof($load) - 7, 6 )) == '.shtml') { ... }

PHP: Regular Expression to get a URL from a string [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Identifying if a URL is present in a string
Php parse links/emails
I'm working on some PHP code which takes input from various sources and needs to find the URLs and save them somewhere. The kind of input that needs to be handled is as follows:
http://www.youtube.com/watch?v=IY2j_GPIqRA
Try google: http://google.com! (note exclamation mark is not part of the URL)
Is http://somesite.com/ down for anyone else?
Output:
http://www.youtube.com/watch?v=IY2j_GPIqRA
http://google.com
http://somesite.com/
I've already borrowed one regular expression from the internet which works, but unfortunately wipes the query string out - not good!
Any help putting together a regular expression, or perhaps another solution to this problem, would be appreciated.
Jan Goyvaerts, Regex Guru, has addressed this issue in his blog. There are quite a few caveats, for example extracting URLs inside parentheses correctly. What you need exactly depends on the "quality" of your input data.
For the examples you provided, \b(?:(?:https?|ftp|file)://|www\.|ftp\.)[-A-Z0-9+&##/%=~_|$?!:,.]*[A-Z0-9+&##/%=~_|$] works when used in case-insensitive mode.
So to find all matches in a multiline string, use
preg_match_all('/\b(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)[-A-Z0-9+&##\/%=~_|$?!:,.]*[A-Z0-9+&##\/%=~_|$]/i', $subject, $result, PREG_PATTERN_ORDER);
$result = $result[0];
Why not try this one. It is the first result of Googling "URL regular expression".
((https?|ftp|gopher|telnet|file|notes|ms-help):((\/\/)|(\\\\))+[\w\d:##%\/;$()~_?\+-=\\\.&]*)
Not PHP, but it should work, I just slightly modified it by escaping forward slashes.
source

Categories