-
Posts
1,832 -
Joined
-
Last visited
-
Days Won
3
Everything posted by salathe
-
[SOLVED] Converting JSON encoded data to javascript object
salathe replied to mikesta707's topic in PHP Coding Help
The result that you're getting looks fine: it is an array of objects, e.g. newData[0] will contain the first object, newData[1] the second, etc.. You can always use Firebug or some other inspector to take a look at the variable and see exactly what is there. Using alert is pretty crude for debugging values. -
What precisely are you aiming to do? Regular expression creation, and programming in general, thrives on having a specific task to accomplish. The original post says you want to match a sequence of characters, yet your own code snippets show that the subject string may contain other things before/after the sequence. Perhaps it would better suit us to be given whatever input you will have, what exactly you want to be done, and some output which should result. For example, do you wish to make sure that a string only contains the type of sequence your original post mentions; to extract that sequence from a string which may also contain other things; something else entirely?
-
PHP Challange / Learning Experance - Basic Criptography
salathe replied to GoneNowBye's topic in PHP Coding Help
c_R\RN^DVDCE^YPTXYCV^Y^YPC_RYBZUREDRARY -
If your input will only contain URLs then the following regular expression should help (or might guide you in a certain direction at least). #.+?(?=http://|(?<!http://)www\.|$)#m An example of its use: $subject = ' www.example.comhttp://www.example.com http://example.com?foo=bar http://www.phpfreaks.comhttp://www.phpfreaks.com '; $pattern = '#.+?(?=http://|(?<!http://)www\.|$)#m'; preg_match_all($pattern, $subject, $matches); print_r($matches); Output: Array ( [0] => Array ( [0] => www.example.com [1] => http://www.example.com [2] => http://example.com?foo=bar [3] => http://www.phpfreaks.com [4] => http://www.phpfreaks.com ) ) Caveat: this will capture anything, it does not look explicitly for URLs. If your string contains other things, it will grab those as well. However, the OP clearly states the subject will be a "string of urls".
-
In your second code block, the value of url will not be a string (it'll be an object for the selection). A quick fix (I guess, not tested) would be: var url = focusedWindow.getSelection().toString();
-
My usual method of entry into the forums is via the "Show unread posts since last visit" link. This gives me a really nice list of threads/posts to browse over and see what catches my eye. Very often, threads do catch my eye and I dive right into them (in a new tab). As soon as that happens it is recorded as "seen". Now, I may read the thread and either post a reply or decide not to do so then will close the tab and continue on elsewhere. The problem is when I come back to the site some time later, or just refresh the "show unread posts" page to see new content, that thread that I'd seen is disappeared (well duh, it has been seen!) unless a new post has popped up (again, duh). Now this is absolutely fine and expected behaviour from a link entitled "Show unread posts since your last visit". However, I'd much, much rather be able to "Show all posts since last visit" (perhaps with some differentiation visually between seen/unseen). That gives me a chance to go back to a topic that might have been interesting but didn't warrant a thorough read or time to spend commenting on the first view. Is this sort of searching actually available right now and I'm just being completely dense and missing a link somewhere? P.S. Long winded post for a very basic question. Sorry!
-
Depending what exactly you need, there might well be several different "answers" to your problem. Do you literally just want to amend the regular expression to accept that second {block}, or any number of {block}s with before/after text, or ... ?
-
I won't deny that things are far from consistent across the language, but things should generally be fairly consistent within different libraries/extensions. In the FAQ, a recent entry (not yet propagated across all mirrors) briefly mentions this problem.
-
The vertical pipe is not necessary, the character class [0-9A-F] will work just as well.
-
I'd hazard a guess that the external website is going to be more of a factor in determining speed (or bottlenecks) versus which of the two approaches you use. It wouldn't take much to code up both methods and properly evaluate them, to find out an answer for yourself for your particular situation.
-
[SOLVED] verifying binary, octal, decimal, or hexidecimal
salathe replied to smerny's topic in Regex Help
Since this in the regex forum, I assume that's what you're looking for. Have you tried anything already: how familiar with regular expressions are you, do you know how to match specific characters? Offtopic The is_numeric() function would accept a wider range of values than just decimal (0-9). For example, exponential parts are allowed for decimal numbers (e.g. "123.45e6" is considered numeric) and it will allow octal and hexadecimal numbers as well (e.g. 0123 octal, 83 decimal and 0x53 hexadecimal are all the same number). -
echoing values from two different arrays alternating
salathe replied to greenheart's topic in PHP Coding Help
With regards to the error, it should be => not just = You could instead make use of the SPL (specifically the ArrayIterator and MultipleIterator) to loop over both arrays at the same time. For example: <?php $titles = new ArrayIterator(array("title 1", "title 2", "title 3")); $descriptions = new ArrayIterator(array("Blah... 1", "Blah... 2", "Blah... 3")); // Use a MultipleIterator to iterate over our two arrays at the same time! $mit = new MultipleIterator(MultipleIterator::MIT_KEYS_ASSOC); $mit->attachIterator($titles, 'title'); $mit->attachIterator($descriptions, 'desc'); foreach ($mit as $it) { echo '<h1>' . $it['title'] . "</h1>\n"; echo '<p>' . $it['desc'] . "</p>\n\n"; } ?> -
Merge them or just combine them into a single object with all of the values (ie, append one to the other)? Merging suggests you want some overlapping values to be overwritten (like array_merge). Samples of the XML would also make life easier than working with print_r output.
-
turn 1.31352737362E+18 into 1313527373619043141
salathe replied to GRMrGecko's topic in PHP Coding Help
Absolutely and as you say, if the OP wants the number as it came through in the JSON then regex would be an easy way to get it. -
preg_match_all('/{([a-zA-Z0-9_]*)}/', $var, $varmatch);
-
The items that you're trying to merge are not arrays, so the error says. What precisely are you trying to merge (a code snippet for us to reproduce the problem is generally useful)?
-
turn 1.31352737362E+18 into 1313527373619043141
salathe replied to GRMrGecko's topic in PHP Coding Help
There shouldn't be a issue with using the number in your script, it's just an issue with how PHP formats large integers for output. You could use number_format() or (s)printf() to format the value as a string. -
Ah fair enough, the OP isn't clear. To the OP: give us an example of the counter file and more of your code (since it appears to just be a snippet that you posted). Does this need to be done with explode or are you open to other techniques?
-
Either don't use double quotes, or escape the dollar sign in the explode lines of your code.
-
You could start off with something like the following, then refine it to suit your particular needs: <?php $subject = " http://php.net is awesome url('http://blah.foo.com') phpfreaks rocks! "; preg_match("#url\('(http://[^']+)'\)#", $subject, $match); $url = $match[1]; echo $url; // http://blah.foo.com ?>
-
[SOLVED] Countdown to first tuesday of every month.
salathe replied to TRemmie's topic in PHP Coding Help
If you're up for using some of the really nice additions in PHP 5.3, then you could also use the following: <?php $now = new DateTime(); $tues = new DateTime('first tuesday of next month'); $diff = $now->diff($tues); echo $diff->days; ?> -
You could do something similar to the following code snippet. They key thing is the use of the SimpleXMLElement::xpath method. <?php // ... foreach ($xmlData->images->image as $image) { // SimpleXMLElement::xpath returns an array, we only want the first value $thumb = current($image->sizes->xpath('size[@name="small"]')); printf( '<img src="%s" width="%d" height="%d" alt="%s"><br>'."\n", $thumb, $thumb['width'], $thumb['height'], htmlspecialchars($image->title) ); } ?> The other option would be to manually loop over the different sizes available until you get to the small one, but the XPath way is much prettier.
-
I don't know about performance (I doubt there would be a huge difference between the two anyway) but the lookaround assertions may be more familiar to people than the magical, mystery \K escape sequence.
-
I think that the forum either ate a blackslash or you missed it out accidentally. For the sake of people visiting this thread to copy/paste, your regex will cause an error ("nothing to repeat at offset 0") since the question mark is still trying to behave as a quantifier even though there is nothing before it. Personally, I would do it ever-so-slightly differently with either: preg_match("/\?>\K.*/s", $input, $match); echo $match[0]; or preg_match("/(?<=\?>).*/s", $input, $match); echo $match[0]; Because: [o]The regex engine won't need to remember the entire match and a capturing group containing the same value. [o]You get regex-fu credits if you use \K and/or a lookbehind assertion. (Bonus power-up for the \K) [o]Neither cause a (regex) compilation failure. [o]It's always nice to see alternative approaches to the same situation.
-
Your code is trying a little too hard to be clever. Simplify. I may be missing some details that you post doesn't make clear but the following might work for you. $e = 3; $h = 4; $values = $_POST[$e][$h];