User Contributed Notes 23 notes

This example lets you parse an unparsed strings variables. Warning: This could cause security leaks if you allow users to pass $variables through this engine. I recommend only using this for your Content Management System.

# For example, unparsed variables like: Test using time: $mytime # This example shows $mytime, and not the actual variable value. # The end result shows the actual variable value of $mytime.

# This is useful for building a content management system, # and directing your variables into your content data, # where content is stored in a file or database, unparsed. # Of course this could slow down page loads, but it's a good way # to parse data from current variables into your loaded new data # making it compatible.

to kristin at greenaple dot on dot ca:thanx for sharing.your function in recursive form proved to be slightly faster and it returns false (as it should) when the character is not found instead of number 0:<?phpfunction strnposr($haystack, $needle, $occurance, $pos = 0) { return ($occurance<2)?strpos($haystack, $needle, $pos):strnposr($haystack,$needle,$occurance-1,strpos($haystack, $needle, $pos) + 1);}?>

Any string can (usually) be thought of as being in one of these 'modes':

pure = what the user actually typed / what you want to see on the page / what is actually stored in the DBgpc = incoming GET, POST or COOKIE datasql = escaped for passing safely to RDBMS via SQL (also, data from DB queries and file reads if you have magic_quotes_runtime on--which is rare)html = safe for html display (htmlentities applied)

if ($longest_common_subsequence[$i][$j] > $largest_size) {// Remember this as the largest$largest_size = $longest_common_subsequence[$i][$j];// Wipe any previous results$return = "";// And then fall through to remember this new value}

if ($longest_common_subsequence[$i][$j] === $largest_size) {// Remember the largest string(s)$return = substr($string_1, $i - $largest_size + 1, $largest_size); } }// Else, $CSL should be set to 0, which it was already initialized to} }

If you want a function to return all text in a string up to the Nth occurrence of a substring, try the below function.

Works in PHP >= 5.

(Pommef provided another sample function for this purpose below, but I believe it is incorrect.)

<?php

// Returns all of $haystack up to (but excluding) the $n_occurrence occurrence of $needle. Therefore:// If there are < $n_occurrence occurrences of $needle in $haystack, the entire string will be returned.// If there are >= $n_occurrence occurrences of $needle in $haystack, the returned string will end before the $n_occurrence'th needle.// This function only makes sense for $n_occurrence >= 1function nsubstr($needle, $haystack, $n_occurrence){// After exploding by $needle, every entry in $arr except (possibly) part of the last entry should have its content returned.$arr = explode($needle,$haystack,$n_occurrence);// Examine last entry in $arr. If it contains $needle, cut out all text except for the text before $needle.$last = count($arr) - 1;$pos_in_last = strpos($arr[$last],$needle);

Note that this function can be combined with wordwrap() to accomplish a routine but fairly difficult web design goal, namely, limiting inline HTML text to a certain number of lines. wordwrap() can break your string using <br>, and then you can use this function to only return text up to the N'th <br>.

You will still have to make a conservative guess of the max number of characters per line with wordwrap(), but you can be more precise than if you were simply truncating a multiple-line string with substr().

//// string strtrmvistl( string str, [int maxlen = 64], // [bool right_justify = false],// [string delimter = "<br>\n"])// // splits a long string into two chunks (a start and an end chunk) // of a given maximum length and seperates them by a given delimeter. // a second chunk can be right-justified within maxlen.// may be used to 'spread' a string over two lines.//

No string-to-array function exists because it is not needed. If you reference a string with an offset like you do with an array, the character at that offset will be return. This is documented in section III.11's "Strings" article under the "String access and modification by character" heading.

I really searched for a function that would do this as I've seen it in other languages but I couldn't find it here. This is particularily useful when combined with substr() to take the first part of a string up to a certain point.