SCRIPTING:

Sara Cox
PHP IMPLEMENTATIONS #1
PHP implementations of javascript functions already sported on this website. Many javascript functions perform tasks whose utility in Php too is obvious, so here a few of them are turned into Php codex.

The details about the inner workings and the usage and the arrangements of the arguments and about what is returned are not covered here: the javascript version documentation coverage of the same function is sufficient: thus only a link to it is provided, and further details are added only if the Php implementation has some particular specificity of its own.
January 2004
{ @ }

The model above is Sara Cox
LOADS OF SARA COX ON THE NET
There already exists one php implementation of a javascript at least: the implemented javascript is the hasher (link with documentation) and its php implementation is here.


THE PHP IMATCH FUNCTION
Its documentation at javascript IMATCH()

This php imatch version differs from its javascript equivalent insomuch as it requires only three arguments and not four, missing the third argument that in its javascript implementation was named flags. Such argument, which in javascript made possible to add modifiers in a string form, is no longer necessary in Php because php requires that all regular expressions are passed as strings anyway.
Remember that even if they are passed as strings, this imatch version uses internally the php built in function named preg_match_all, and thus the $regexp argument, though a string, should carry both the leading and trailing forward slashes that all regular expressions want, though within the apexes; instance:
imatch("text to search for foo instances" "/foo/i", 2);
I have anyway added a few checks that would insert them if missing (both or either of the slashes).

This implementation returns false in case no match is found or some input is unsuitable (the javascript version returned null in such cases).

Do not add modifier g to your regular expression: the function used within has global scope by default. Normally, adding the g modifier to the preg_match_all function would trigger errors in php; anyway I added in this function a check that would strip modifiers from the incoming regular expression if by mistake you included them.

This php implementation also returns false if the regular expression checks for empty strings like: "//", because such empty expressions would just be basically meaningless (it means the regular expressions searches for... nothing or, better, for whatever is between letters, namely nothing) but would feed the internal strpos function with an empty string as its second argument, which in php triggers errors; under this point of view the javascript implementation would cause no such hassles, yet it should be ackowledged that searching for nothing doesn't make much sense though javascripts in this issue shows a superiority for it can deal with this exception without causing disturbances.

Note: a thank you goes to the guys of php.net who helped me solving an apparent problem that I misjudged as such at 5 in the morning after 15 hours of coding.

a Joan Miro painting
a Joan Miro painting: Bleu II, (1961)
IMATCH
The model on the right is Linda O' Neil
LINDA O' NEIL ON THE NET


Linda O' Neil


THE PHP WORDWRAPPER FUNCTION
Its documentation at javascript wordWrapper()

This php wordWrapper version differs from its javascript equivalent insomuch as it requires only five arguments and not six, missing the fifth argument that in its javascript implementation was named flags. Such argument, which in javascript made possible to add modifiers in a stringed version of a regular expression, is no longer necessary in Php because php requires that all regular expressions are passed as strings anyway.
Also, note that the name of this function is wordWrapper and not wordWrap which would be a built in existing php function.

To make it case insensitive, remember to add the i flag in your stringed regular expression, instance:
"/Hallo/i"
to match, in our case, both lowercased "hallo" and whatever uppercased combination of the same word such as "HalLo".
Since the function relies on capturing, you should not forget to parenthesize your $regexp argument (wider explanation in the javascript wordWrapper documentation). Like its javascript version, also this php version can detect if not veven one set of parenthesis is present, and in such case it envelopes the core of the regular expression within parenthesis to ensure that the capture process occurs.

You must refer to the javascript wordWrapper documentation to understand the following: there is no difference as far as the $complex argument is concerned, except that the $1, $2, $3, $x variables must be in a slightly different form to be php compliant, namely
  • either with the dollar sign escaped namely with a backward slash prepended, example:
    \$1
    which is valid syntax for php from php version 4.0.3 onward.
  • in the shape of two backward slashes followed by the number; in such case we could say the second backward slash takes the place of the dollar sign, example:
    \\1
    This applies below php version 4.0.3
Example without the $complex argument:
$string="LO! HALLO WORLD HALLO";
wordwrapper($string,"/(Hallo)/i","<strong>","</strong>");

Or, with the complex argument:
$string="LO! HALLO WORLD HALLO";
wordwrapper($string,"/(Ha)(ll)(o))/i","<strong>","</strong>",
"\\1<strong>\\2</strong>\\3);

Note that if you set the $complex argument, just like its javascript version whatever you pass as $before and $after will be ignored.

a Joan Miro painting
a Joan Miro painting: Night shapes drawn forth by the trails of the snails, (1940)
WORDWRAPPER
The model on the right is Michelle Clack
MICHELLE CLACK ON THE NET


Michelle Clack


THE PHP STRINGEXTRACTOR FUNCTION
Its documentation at javascript stringExtractor()

Like its javascript original implementation, the scripts requires that you define within your codex also the imatch function (its php implementation is above in this same file). If you forget to include it, the function won't work.

Unlike the javascript version that has a caseInsensitive argument, this php function has no such argument.
Its $text, $before, $after arguments must be stringed regular expressions. To make them case insensitive it is up to you to append the i modifier to your stringed rgular expressions, example:
"/hallos/i"
If by mistake you forget appending and prepending the forward slashes, the function makes a check to verify whether there is none, and in such case appends and prepends them to $text and $before and $after as well.

a Joan Miro painting
a Joan Miro painting: Women sorrounded by the flight of a bird, (1940)
STRINGEXTRACTOR
The model on the right is Dena Doster
DENA DOSTER ON THE NET


Dena Doster



THE PHP STRINGSHORTENER FUNCTION
Its documentation at javascript stringShortener()

This is the php impementation of the string shortener.

a Marc Chagall painting
a Marc Chagall painting: Abraham and the three angels, (1960-1966)
STRINGSHORTENER
Photo on the right by Andy Chabot
Andy Chabot's Website
ANDY CHABOT ON THE NET


Andy Chabot