thebadbad
Members-
Posts
1,613 -
Joined
-
Last visited
Everything posted by thebadbad
-
Have a look at this post: http://www.phpfreaks.com/forums/index.php/topic,276359.msg1307247.html#msg1307247
-
That's so straight forward, why didn't I think of that?
-
Or preg_replace('~<span\b[^>]+\bclass\s?=\s?([\'"])eventname\1[^>]*>(.*?)</span>~is', '<strong>$2</strong>', $input)
-
rajiv is right, you can check the range in the last table on this page: http://www.ascii-code.com/ Edit: And you can output the range using range(): <?php print_r(range("\x7f", "\xff")); ?>
-
I would probably do that with two separate patterns: <?php //get image link(s) preg_match('~<img src="http://www\.discshop\.se/shop/img/omslag/front_normal/([^"]+)"~i', $html, $match); //build image URLs $thumb = 'http://www.discshop.se/shop/img/omslag/front_normal/' . $match[1]; $full = 'http://www.discshop.se/shop/img/omslag/front_large/' . $match[1]; //get price preg_match('~<span class="price "[^>]*><span\b[^>]*>([^:]+):~i', $html, $match); $price = $match[1]; ?>
-
I'm with Boom; it looks like quoted-printable. If it's not, what is it then? Where does it come from? You have to know the encoding before you can decode it.
-
The OP says he want to write it to a file. The easiest way to do this would be using the nowdoc syntax to specify the string, but that requires PHP 5.3.0. Any other method potentially involves a lot of escaping.
-
Sure, you can e.g. output the data in a table (in its most simple form in this example): <?php echo '<table>'; foreach ($data as $key => $val) { echo "\n\t<tr>\n\t\t<td>$key</td><td>" . implode('<br />', $val) . "</td>\n\t</tr>"; } echo "\n</table>"; ?>
-
You need to read the contents of the file into the variable. Either use file_get_contents() or cURL. And I noticed that the site uses <br/> instead of <br> for line breaks, so you'll have to change that in the code (the parameter for explode() inside the foreach loop).
-
AFAIK that's only possible if the visiting computer is on the same physical network as the server.
-
Assuming the source looks more or less strictly like that for every film/series, here's a way to do it: <?php //$html holds the source code preg_match_all('~<div class="item1"><b class="txt_grey">([^:]+):</b></div>\s*<div class="item2">(.*?)</div>~is', $html, $matches, PREG_SET_ORDER); $data = array(); foreach ($matches as $arr) { $data[$arr[1]] = explode('<br>', $arr[2]); foreach ($data[$arr[1]] as $key => &$value) { $value = trim(strip_tags($value)); if ($value == '') { unset($data[$arr[1]][$key]); } } } echo '<pre>' . print_r($data, true) . '</pre>'; ?> Output: Array ( [svensk titel] => Array ( [0] => Familjen Macahan ) [Originaltitel] => Array ( [0] => How the West Was Won ) [Genre] => Array ( [0] => TV-serie ) [underkategori] => Array ( [0] => Äventyr [1] => Kult (60-80-tal) ) [Produktionsland] => Array ( [0] => USA ) [inspelningsår] => Array ( [0] => 1977-1979 ) [skådespelare] => Array ( [0] => James Arness [1] => Bruce Boxleitner [2] => Eva Marie Saint [3] => Kathryn Holcomb [4] => William Kirby Cullen [5] => Vicki Schreck ) [Åldersgräns] => Array ( [0] => 15 år. ) )
-
How To Make More Than One Redirection with PHP on the same page?
thebadbad replied to Darkwoods's topic in PHP Coding Help
Using array_rand() instead of rand() and count(): <?php $arr = array( 'http://www.site1.com/new_page.html', 'http://www.site2.com/new_page.html', 'http://www.site3.com/new_page.html' ); header('Location: ' . $arr[array_rand($arr)]); ?> -
Simply don't echo/print/var_dump()/print_r() the array. Via a cron job.
-
<?php if ($match[1] == 'open') { echo 'Glowing'; } ?> But note that $match[1] won't exist when the pattern doesn't match the source, resulting in a thrown notice in those cases. That's possible yes, but problematic. Some servers ban your server's IP if they suspect you're automating a lot of requests. And the script would halt until the state changed to "open", and probably time out, depending on your server settings. A more realistic approach would be to make a request every 5 minutes e.g.
-
It works, as in runs without syntax errors, but the output is a total mess and includes parts of what's supposed to be PHP code.
-
It's not only valid but required when it's used like that, in the middle of a larger string.
-
A way to take care of the casing: <?php $str = 'Groups you own Your groups Create a new group'; function _callback($matches) { $matches[0] = 'organization'; if (ctype_upper($matches[1])) { $matches[0] = ucwords($matches[0]); } return ($matches[2] ? $matches[0] . 's' : $matches[0]); } $str = preg_replace_callback('~\b(g)roup(s?)\b~i', '_callback', $str); echo $str; ?>
-
Well, isn't it obvious? You're not closing the anchor tag.
-
Word boundaries were made just for this. Example: <?php $str = 'The whitest color is white.'; echo preg_replace('~\bwhite\b~i', 'black', $str); ?>
-
I would probably do it like this then: <?php $replace = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Current Conditions'); $string = preg_replace('~(<title>(?:' . implode('|', $replace) . ')[^:]*):[^<]+~i', '$1', $string); ?> Just make sure that the string in $replace doesn't contain any special regex chars or the delimiter in use.
-
Variables aren't translated inside single quotes.
-
That's only part of a larger string, right? Assuming the 'variables' are non-whitespace, try this: <?php echo preg_replace('~(Sunday): \S+ Low \S+ High \S+~i', '$1', $string); ?> If the string you provided is the full string, you could simply do <?php echo substr($string, 0, strpos($string, ':')); ?>
-
Ah, thought it might have something to do with the version.
-
Clever, although it complaints that func_get_args() can't be used as a function parameter