thebadbad
Members-
Posts
1,613 -
Joined
-
Last visited
Everything posted by thebadbad
-
Strongest hash function available to standard PHP?
thebadbad replied to DWilliams's topic in PHP Coding Help
Algorithms in the SHA-2 family are recommended by NIST. Remember that your salt should be a random string with at least as many variable bits, as there are bits in the hash result. Then you're good to go! -
He's not looking to accept alphanumeric characters only:
-
As long as you encode the text with htmlentities() or similar, it should be valid.
-
The way I would go about it: $img = preg_replace('~<img\b[^>]*\bsrc\s?=\s?([\'"])cid:[^\1]*\1[^>]*>~i', '', $img);
-
preg_match('~\$Revision: (\S+)~', $this->revision, $rev); \S is a shorthand character class matching any non-whitespace character. The only other difference is the use of pattern delimiters (tildes in my case), which is required with preg_match().
-
Or if you want to use the external page's title (if applicable): function _callback($matches) { $html = file_get_contents($matches[0]); if ($html != false) { if (preg_match('~<title\b[^>]*>(.+?)</title>~is', $html, $match)) { return '<a href="' . $matches[0] . '">' . $match[1] . '</a>'; } } return '<a href="' . $matches[0] . '">' . parse_url($matches[0], PHP_URL_HOST) . '</a>'; } $ret = preg_replace_callback('~\bhttp://\S+(?![^<]*?>)~i', '_callback', $ret);
-
Quick example: function _callback($matches) { return '<a href="' . $matches[0] . '">' . parse_url($matches[0], PHP_URL_HOST) . '</a>'; } $ret = preg_replace_callback('~\bhttp://\S+(?![^<]*?>)~i', '_callback', $ret);
-
One way you can do it, although it's not very elegant: $content = 'Testing a test with this! Tag: <span title="test">tag</span>. <h1>Test heading</h1>'; //replace keywords (that are not part of HTML tags) $content = preg_replace('~\btest\b(?![^<]*?>)~i', '<a href="" UNIQUE>$0</a>', $content); //remove created links between heading tags function _callback($matches) { return preg_replace('~<a href="[^"]*" UNIQUE>(.*?)</a>~s', '$1', $matches[0]); } $content = preg_replace_callback('~<h([1-6])\b[^>]*>.+?</h\1>~is', '_callback', $content); //remove UNIQUE marks $content = preg_replace('~(<a href="[^"]*") UNIQUE>~', '$1>', $content); header('Content-type: text/plain; charset=utf-8'); echo $content;
-
You should be able to use get_meta_tags() for the META tags (else the comments on that page also contains regex solutions). You can check for iframes with this: //$html contains page source code if (preg_match_all('~<iframe\b[^>]+src\s?=\s?([\'"])(.+?)\1[^>]*>~is', $html, $matches)) { echo '<pre>' . print_r($matches[2], true) . '</pre>'; } else { echo 'No iframes found.'; }
-
ctype_digit() will strictly check for numbers (in a string) only.
-
Blocking an IP address - proper formatting of the ereg expression
thebadbad replied to chito's topic in Regex Help
The fastest way to check if it begins with 202. is using substr(): if (substr($remoteaddr, 0, 4) == '202.') { //... } The regex approach could be if (preg_match('~^202\.~', $remoteaddr)) { //... } -
$str = ' <h2 style="color: #383737; margin-bottom: 3px; text-transform: capitalize;">Details</h2><br/> <h2>input</h2><br/>'; preg_match('~>Details</h2><br/>\s*<h2>(.*?)</h2><br/>~is', $str, $match); echo $match[1]; \s* matches zero or more whitespace characters (including line breaks).
-
When you're just replacing simple text, you can achieve this effect with strtr(). But I'm not sure how to get it done with regular expressions involved. $str = 'this string contains this body of text and works for an example'; $replace = array( 'this body of text' => '<span class="highlight">this body of text</span>', 'this body' => '<span class="highlight">this body</span>', 'body of text' => '<span class="highlight">body of text</span>', 'body' => '<span class="highlight">body</span>' ); echo strtr($str, $replace); //this string contains <span class="highlight">this body of text</span> and works for an example
-
Using preg_replace to replace contents into 2 locations
thebadbad replied to pandyboy's topic in Regex Help
No worries. In that case you can define an array of patterns and replacements: $replace = array( '~\[\[#([0-9]+)#\]\](.*?)\[\[end\]\]~is' => '<a href="javascript:;" onclick="Show_Stuff(\'$1\');">Show</a> <div style="display:none;" id="$1">$2</div><br />', '~\[\*(.*?)\*\]~s' => '<strong>$1</strong>', '#\[~(.*?)~\]#s' => '<em>$1</em>' ); $str = preg_replace(array_keys($replace), $replace, $str); I used strong and em tags since the b and i tags are deprecated. -
Using preg_replace to replace contents into 2 locations
thebadbad replied to pandyboy's topic in Regex Help
I would just go with a simple <?php $str = '[[#201001251351#]]Some text here[[end]] Something else in this bit [[#201001251353#]]Some different text here[[end]] And more different bits [[#201001251357#]]Some more here[[end]] Further different bits which may include other replaced things'; $replace = '<a href="javascript:;" onclick="Show_Stuff(\'$1\');">Show</a> <div style="display:none;" id="$1">$2</div><br />'; echo preg_replace('~\[\[#([0-9]+)#\]\](.*?)\[\[end\]\]~is', $replace, $str); ?> -
Not very elegant, but does the job: <?php $array = array('a' => 'A', 'b' => 'B', 'c' => 'C', 'd' => 'D'); $key = 'c'; $val = $array[$key]; unset($array[$key]); $array = array_merge(array($key => $val), $array); //you can also simply add the arrays here instead; array($key => $val) + $array echo '<pre>' . print_r($array, true) . '</pre>'; ?>
-
<?php $str = 'I am going [ABCDEF]123[/ABCDEF] to movie.'; preg_match_all('~\[ABCDEF\]([0-9]+)\[/ABCDEF\]~', $str, $matches); echo '<pre>' . print_r($matches[1], true) . '</pre>'; ?>
-
The only thing you need is to add a pair of pattern delimiters (I most often use the tilde ~) and the pattern modifier i, making the search case insensitive (to achieve the same effect as eregi() - but that won't have any actual effect with the current pattern, since both a-z and A-Z are included. In the pattern below I've removed A-Z and added the i). I would also add the pattern modifier D, to make the dollar sign really mean end of string (read here). ~^[a-z0-9._-]{4,12}$~iD
-
All of your URLs except the first one have whitespace in front of them. Either remove the whitespace or manually construct a proper array.
-
You forgot to load the source code of the page. <?php $urls = array('http://www.megaupload.com/?d=ZD6ACN1J', 'http://www.megaupload.com/?d=ZACN1J'); foreach ($urls as $url) { $source = file_get_contents($url); echo "<strong>$url</strong>: "; if (preg_match('~<input\b[^>]*>~i', $source)) { echo '<span style="color: green;">File is available.</span><br />'; } else { echo '<span style="color: red;">File does not exist.</span><br />'; } } ?> Megaupload serves a language specific error message, so I search for an input tag instead of the phrase in your code.
-
You simply have to find a way do distinguish a page with a 'dead' file from a page with a live file. A Rapidshare page e.g. contains the header <h1>FILE DOWNLOAD</h1> when the file is live. You can simply use strpos() to check if that's present in the source code: <?php $source = file_get_contents('http://rapidshare.com/files/326980450/TOIOU_NIN_AVOTT_GIFT_1080p.part01.rar'); if (strpos($source, '<h1>FILE DOWNLOAD</h1>') !== false) { //file is live } ?>
-
Problems with preg_match and user input that contains parenthesis
thebadbad replied to cyberRobot's topic in Regex Help
preg_quote() is made for this And remember to feed your delimiter as the second parameter. Edit: But if you only want to do a case-insensitive comparison, the below will suffice (and be faster): <?php if (strtolower($newPhoneNum) == strtolower($databasePhoneNum)) { //match } ?> or probably even <?php if (strcasecmp($newPhoneNum, $databasePhoneNum) == 0) { //match } ?> -
Regex Experts...I need help excluding <html> tags...!
thebadbad replied to steven fullman's topic in Regex Help
@OP, you can find an alternative pattern that uses a look-behind instead of a look-ahead in this post: http://www.phpfreaks.com/forums/index.php/topic,258333.msg1215689.html#msg1215689 -
Yes, that would work. And it's actually really simple to do it (given the relative2absolute() function), and by far the best solution as far as I know. echo relative2absolute('http://example.com/folder/page.php', '../relative/link/file.php'); //http://example.com/relative/link/file.php
-
@OP Why aren't you using the method from the link I posted? It's much more robust.