Posts Tagged ‘string’

Getting the Text Between HTML Tags in PHP

Suppose you’re automatically parsing a webpage, and you come across the following kind of thing:
blah blah
some starting text
some useful content
some ending text
blah blah
We want to parse out the useful content from among the non-useful stuff, and we know there’s some starting text and some ending text that wraps the useful content.
A better example:
I like chicken
<div [...]

Formatting Strings for URLs

Here’s a quick Ruby snippet you can use to format a string to be used in a URL:
string.downcase.gsub(/[^a-z0-9]+/i, '_').chomp('_')
This turns something like
This is a Post URL!
into
this_is_a_post_url
which you can use in your archive URLs (like http://www.website.com/article/this_is_a_post_url)
The string is first converted to all lowercase letters with the downcase method of the String class.
Next, we use gsub [...]