Visual Regex

Posted by Jetlogs @ 2:49 pm
Category: Web Development

Regex: its usually a bane for most developers and programmers to the point that most regex patterns in the wild are usually cut and paste from the web or text books. Even I admit that I’m guilty of this.

However, wouldn’t it be better if we learn how the regex pattern we input works? Good thing is, there is already such a thing! The website strfriend transforms a RegEx pattern into a simple chart that is easier to understand.

Here is an example:
Visual Regex
This is actually from a RegEx I used for matching all XML schema elements and attribute groups that doesn’t use a closing tag that have a ref attribute.

<xs:(element|attributeGroup) ref=”[^"\r\n]*”[^\r\n/>]*/>

For those who are curious, here is the other RegEx I used for matching those XML schema elements / attribute groups with closing tags that have a ref attribute:

<xs:(element|attributeGroup) ref=”[^"\r\n]*”[^\r\n/>]*>(.|\r\n|\n)+?</xs:(element|attributeGroup)>


PHP: SimpleXML XPaths and Namespaces

Posted by Jetlogs @ 7:36 pm
Category: PHP, Web Development

Ever since PHP version 5.2.0, XML parsing has been made so much easier with the new SimpleXMLElement->xpath() function. No longer do we have to parse an XML document by working our way through all the child node, we just have to know the XML element’s name

Assuming we have an XML string assigned to $xml_string

$xml_string='<?xml version="1.0" encoding="utf-8"?>
<a>
	<b>
		<c>
			<d>
				<e>Hello World</e>
			</d>
		</c>
	</b>
</a>';

$xml = new SimpleXMLElement($xml_string);

Here is how it was done before 5.2.0 to access the <e> element

echo $xml->b->c->d->e[0]; //prints Hello World

Now with the new SimpleXMLElement->xpath() function, here is how it is done:

$result = $xml->xpath('//e');
echo $result[0]; //prints Hello World

Read more »


  • Archives

  • Donations

  • Social Bookmarks

  • Jetlogs.org
    Some Rights Reserved 2007
    Creative Commons License