-
Posts
1,832 -
Joined
-
Last visited
-
Days Won
3
Everything posted by salathe
-
I know you've asked for code samples but are you literally looking for someone to code a basic script which does exactly what you want, or just some ideas that you can take, research and write code for yourself?
-
By default the dot won't match newlines, to make it do so you need to add the s modifier to the end of your pattern. You'll also probably want to use (.+) rather than (.)+ as the latter will only capture the very last character that it can find.
-
Preg_replace. Replace everything that does NOT match?
salathe replied to king.oslo's topic in Regex Help
The preg_match (and preg_match_all) are designed for that sort of task. If you want a simple function to grab the matched value quickly then something like the following might work: function preg_grab($pattern, $subject, $group = 0) { if (preg_match($pattern, $subject, $match) AND isset($match[$group]) { return $match[$group]; } return FALSE; } Then you can call that in a similar way to preg_replace like: $string = 'Marius Jackson <m.jackson@example.com>'; echo preg_grab($email_pattern, $string); // m.jackson@example.com echo preg_grab('/Marius (\w+)/', $string, 1); // Jackson -
[ot] Not particularly. Just in case anyone was wondering, here is one way to parse the required information using the DOM/XPath. <?php $url = 'http://www.rutebok.no/NRIIISStaticTables/Tables/ruter/index/Avd_01.htm'; $dom = new DOMDocument; // HTML will have lots of XML errors, ignore them when loading libxml_use_internal_errors(TRUE); $dom->loadHTMLFile($url); libxml_use_internal_errors(FALSE); // Grab the location (Halden) from the JavaScript $script = $dom->getElementsByTagName('script')->item(2)->textContent; $location = 'Unknown'; if (preg_match('/^\["([^"]+)", "Tab01"/m', $script, $match)) { $location = $match[1]; } // Query the first tab for the routes $xpath = new DOMXPath($dom); $tab = $xpath->query('//div[@id="Tab01"]')->item(0); $rows = $xpath->query('./table[2]/tr/td/table/tr', $tab); $routes = array(); foreach ($rows as $row) { $cells = $row->getElementsByTagName("td"); $routes[] = array( 'number' => $cells->item(1)->textContent, 'name' => str_replace("\r\n", "", $cells->item(2)->textContent) ); } // Output routes header('Content-Type: text/html; charset=utf-8'); ?> <h2><?php echo $location ?></h2> <?php if (empty($routes)) : ?> <p>No routes found :-(</p> <?php else : ?> <?php foreach ($routes as $route) : ?> <strong><?php echo $route['number'] ?></strong> <?php echo $route['name'] ?> <strong><?php echo $location ?></strong> <br> <?php endforeach; ?> <?php endif; ?> [Edited to fix super-long HTML line][/ot]
-
The same key will not be returned more than once from array_rand: so yes, in that case they would be unique.
-
The portion of code below the "complete" label will always be executed, just because there is a label before it does not make that code special in any way. The label just acts as a point that the script can say "hey, skip along and execute this bit right away" and not as a place that says "only execute this code if I've been told to do so".
-
Can it be done? Sure, not with REGEXP though (you'd need to use string functions). However, you'd be much better off extracting that ID number and storing it in another column which can be indexed.
-
Hehe no problem. Glad to see the problem solved.
-
You'll probably want to break out of the foreach loop upon finding a successful match by using the keyword continue.
-
Numbers of characters in A4 Size paper in microsoft office
salathe replied to zohab's topic in Miscellaneous
The font, text size, characters, kerning, line-height, margins, and so on will all affect how many characters can fit on the page. If you can only get 260 characters on an A4 page, the text must be huge! 260 characters is the same amount of text as this reply! -
The solution will vary depending on precisely what you're wanting to do. Do you want to get one key from the database and see if there is a (jpg, gif, whatver) file matching that key, or do you want to do similar with many (all?) keys, or..?
-
Your attached HTML file and the sample HTML do not correlate and your PHP code doesn't appear to match the structure of either of them. That said, I took the source code from this page because that looks to be more useful. The following snippet is then able to extract the techspec/value pairs from the HTML table (your HTML/PHP might be different so this may not be simply copy/paste). // Get product specification table rows $prod_spec_nodes = $xpath->query('//tr[@class="techspec b"]/parent::table/tr[td[2][starts-with(@class, "techspec")]]'); $group = ''; foreach ($prod_spec_nodes as $node) { $spec = $xpath->query('.//td[@class="techspec"]', $node); $value = $xpath->query('.//td[@class="techvalue"]', $node); if ($spec->length == 1 AND $value->length == 1) { $spec = trim($spec->item(0)->nodeValue); $value = trim($value->item(0)->nodeValue); add_specs(array('productid' => 12345, 'group' => $group, 'left' => $spec, 'right' => $value)); } } function add_specs($values) { var_dump($values); } [ot] Part of the "no-one helping" could well have been due to the mismatching source, or due to the nature of the question (perhaps people run away when they see "XPath") or, as is increasingly the case for me, they view the thread once and intend to come back and have a proper look at the problem but the thread disappears from the list when they next view the forum. [/ot]
-
Have you tried glob($month . '/*.{jpg,JPG,gif,GIF}', GLOB_BRACE) ?
-
Look in the manual for GLOB_BRACE.
-
The main issue is that the pattern matches when there are only "word" characters between hello and world. In your example above, there are multiple non-word characters (spaces) there.
-
It's not a particularly good example of easy-to-understand code.
-
It does remove the "foo" value from the array so the foreach loop will only loop over the remaining two values ("bar" and "baz"). Note, that the $counter is given the length of $arg1 (which is "foo") and then the two array value lengths are added.
-
Give the following a go: ([a-zA-Z0-9-]+) Your post didn't say you want numbers too, but your try did... so my regular expression allows numbers.
-
The OP never said that value was visible in his script or as the URL being visited. arbitter, are you still having problems and if so can you give us more precise details in order to help us help you.
-
$_GET values are automatically decoded. Unless the query string contains double-encoded values, calling urldecode is not necessary.
-
How about if ($time < strtotime('-7 days')) …?
-
foreach (glob('*',GLOB_ONLYDIR) as $map) { $qmap = urlencode($map); echo "<tr><td><a href='?month=$qmap'>$map</a></td></tr>"; } The single quote in the variable's value was causing your HTML to be broken. Values going into URLs should be urlencoded so that the constructed URL contains only URL-safe characters (and has the added bonus of fixing your HTML issue).
-
teamatomic, all $_POST values are strings anway (unless altered in your script) so that would be redundant (plus it's proper to use (string) to cast to a string value).
-
That's not how files work I'm afraid. Whilst it would be possible if the search and replace values were of the same byte length, in most cases the very minimum needed is to rewrite the rest of the file from the search point onwards. With small files, overwriting the entire contents isn't anything to worry about anyway.