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" , 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.
THE PHP WORDWRAPPER FUNCTION
|
|
|
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.
THE PHP STRINGEXTRACTOR FUNCTION
|
|
|
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.
THE PHP STRINGSHORTENER FUNCTION
|
|
|
This is the php impementation of the string shortener.