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') { ... }
Related
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().
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.
I have this regular expression:
preg_match_all("/<a\s.*?href\s*=\s*['|\"](.*?)(?=#|\"|')/si", $data, $matches);
to find all urls, it works fine, BUT how can I modificate it to find urls with question marks ONLY?
Example:
0123
And preg_match_all will return:
http://site.com/index.php?id=1
http://site.com/calc/index.php?id=1&scheme=Venus
preg_match_all("#<a\s*href\s*=[\'\"]([^\'\"]+\?[^\'\"]+)[\'\"]#si", $data, $matches);
Try this.
Don't try to make everything happen in one regex. Use your existing method, and then separately check the URL that you get back to see if it has a question mark in it.
That said, don't use regular expressions to parse HTML. You cannot reliably parse HTML with regular expressions, and you will face sorrow and frustration down the road. As soon as the HTML changes from your expectations, your code will be broken. See http://htmlparsing.com/php for examples of how to properly parse HTML with PHP modules that have already been written, tested and debugged.
Andy Lester gave you the answer with right thing to do.
Here's your regex though:
<a\s.*?href\s*=\s*['|\"](.*?\?.*?)(?=#|\"|')
as seen here:
http://rubular.com/r/LHi11VMMR9
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Replace URLs in text with HTML links
I'm writing some code to convert plain-text links to HyperText links in PHP. I'm using a regular expression (of which I know almost nothing) and I'm coming up against some problems:
I don't know how to conditionally include/exclude an item in the expression
I don't know how to include URL parameters in my expression
I don't know much about RegEx!
This is the code I've written:
function text2link($text)
{
$text = preg_replace
(
"/(ftp\.|www\.|http:\/\/|https:\/\/|)(.*)(\.)(com|net|co\.uk)/i",
"<a href='http://$2$3$4' target='_blank'>$1$2$3$4</a>",
$text
);
return $text;
}
This code will convert the following "styles" of hyperlinks:
http://www.google.com
www.google.com
google.com
etc., however, the links are forced to HTTP. No matter what you type in, the expression forces HTTP. Is is possible to conditionally include the necessary protocol? For example:
"<a href='[SOME IF-CONDITION GOES HERE TO DECIDE WHAT PROTOCOL TO USE]$2$3$4' target='_blank'>$1$2$3$4</a>",
This condition would be something along the lines of:
if $1 == "http, else if $1 == "https", else if $1 == "ftp",
etc., etc.
Also I realise I'm not accounting for anywhere near enough extensions, this is just test code right now.
Edit: I should note that I'm not looking for "fool proof", unbreakable solutions requiring 200 lines. I don't mind if some obscure domains don't get picked up. I just want the most common domains and URLs to get detected and converted to clickable links.
You don't need to tweak your regex:
function text2link($text, $protocol="http://")
{
$text = preg_replace
(
"/(ftp\.|www\.|http:\/\/|https:\/\/|)(.*)(\.)(com|net|co\.uk)/i",
"<a href='$protocol$2$3$4' target='_blank'>$1$2$3$4</a>",
$text
);
return $text;
}
You can pass in an optional protocol as a second parameter, and it'll default to http:// if you don't.
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