CDATA, With, Match and Regular Expression

CDATA

Sometimes when you get errors within your HTML validation can sometimes just get bogged down with javascript code that has not been “told” to be ignored by the parser, for example the javascript code

if (a < b)

is fine within that context, but within HTML the < could be a start of a tag and thus would fall over with some old parsers and also the validator, so best to wrap the javascript code within the CDATA

<script type="text/javascript">
<![CDATA[
   if (a < b) 
...
]]>
</script>

WITH

The with syntax will allow you to work on a object of a HTML form elements within having to constantly referencing the object fully, for example if you had a HTML as

<form name="theform">
<input id="theinput"/> 
<input id="secondinput"/>
</form>

and so within the javascript you could do something like this without using the with statement,

var theinput = document.form.theform.theinput;

well if you wanted to you could use the “with” syntax and thus if you wanted to access all of the elements within that HTML object then

with (document.form.theform)
{
    var first = theinput;
    var second = secondinput;
}

just allot more nicer to use the objects :).

Match / Regular expression

Since a string within the javascript is capable of having regular expression actions applied to it, you can use the inbuilt method that is associated with the string class as such, which allows you to do regular expression, but for some reason you do not need to put in the ” ” around the regular expression.

var username = document.getElementById("username");
if (
    username.match(/.+@.+\.com$)
   ) {
....

will match with a regular expression so that the username has to end with .com for it to be valid!!.. not really a good test, but there you go!.

preg_match – regular expression

Here is a good link for a cheat sheet for regular expressions that if you forget some of the syntax.

Here are some quick notes about regular expression in PHP that allow you to pull out information from a string of text.

$stringofvalues = "hi there from me - and now after that lets!!";
preg_match("/^.* - (.*)!!/", $stringofvalues, $matches);
print_r($matches);

and the output would be

Array
(
    [0] => hi there from me - and now after that lets!!
    [1] => and now after that lets
)

where the first array [0] is always the string that was passed in, and any matches that you picked are in the following index’s in the array (because the first match [0] in the array is the real string because it has matched that already 🙂 ) and then the second matches that you want to check for are within the regular expression ( .. ) brackets, so in my regular expression

/^.* - (.*)!!/

this means start at the beginning of the string ‘^’ and then any character ‘.’ 0 or more times ‘*’ find un-till you find a string as ” – ” (which is within the string to be searched) and then now copy any character 0 or more times ‘.*’ but this time place it into a matched index within the array by using the () around that regular expression search.

So if you wanted to pull back 2 matches from that string, then you could do something like

preg_match("/^(.*) - (.*)!!/", $stringofvalues, $matches);
print_r($matches);

Which the output would be

Array
(
    [0] => hi there from me - and now after that lets!!
    [1] => hi there from me
    [2] => and now after that lets
)

where the first part of the string (before the ” – “) is within the 1st index of the matches array and then the second search match is within the 2nd index of the matches array.

Regular expression – PHP

Regular expression is when you have a match condition and a string to search through. Here is a cheat sheet to download to view regular expressions syntax.

The regular matching method in php that I use is preg_match_all

int preg_match_all ( string pattern, string subject, array &matches [, int flags [, int offset]] )

which basically means that it will return a integer value of how many matches it has found within the subject text. The flags and offset are optional values.

Here is a example to pull out HTML A links within a text, A links start with <a and have some optional parameters e.g. href, title >text to display on the screen</a> (the closing tag. So in xml speak as such

<a[optional attributes]>value</a>

So the basics of searching for HTML A links within a string is to look for “” which then has the value and then the closing tag . So to put that in regular expression talk, to break it down we first search for the start <a followed by x amount of characters \s.* with a ending tag of <\/a> the “\” is there because the / is a condition statement for regular expression. And with this together it would look like

<a\s.*<\/a>

But because <\/a> happens twice in the string and either one can be the end point of the search since we are searching string values “\s.*” 0-x amount of them. \s means white space character “.” means any character that is not \n and “*” means 0 – more times of the previous check. So there is potential for a problem here we are not telling the regular expression to stop at the first < character and to this we use the [] to match some characters, the ^ in this context means any character not in this list about to come and "<" is the stopping character. So the full regular expression will be.

<a\s.[^<]*<\/a>

Here is the php code that will display all of the HTML A links from a string.

<?php
// here is a string with 2 a href links inside it and sounding text.
$matchAStr = "coding friends is my site :) <a href=\"http://www.codingfriends.com\">codingfriends.com</a> hi there.. " . 
	    " mountains are nice. <a href=\"http://www.norfolkhospice.org.uk/\">norfolkhospice.org.uk</a>" . 
	    " support the tapping house in Norfolk";
 
// the preg_match_all uses regular expression to match and pull out strings, $matches is the matches found.
 
$matched = preg_match_all("/<a\s.[^<]*<\/a>/",$matchAStr,$matches);
echo "There was $matched matche(s)\n";
 
echo "<ol>\n";
foreach($matches[0] as $url)
{
  echo "<li>".$url."</li>\n";
}
echo "</ol>\n";
?>
There was 2 matche(s)
<ol>
<li><a href="http://www.codingfriends.com">codingfriends.com</a></li>
<li><a href="http://www.norfolkhospice.org.uk/">norfolkhospice.org.uk</a></li>
</ol>