I am using wordpress to allow a client to enter a curriculum vitae and have it output the correct and consistent html formatting:
So I have a plain text box custom field and am having them enter in the content in the following way:
[section "Solo Exhibitions"]
[year 2011]
Inner Space
January 11
Peter Pan
February 11
[section "Group Shows"]
[year 2009]
Group Show
January 09
Big Group Show
February 09
So on and so forth. I wrote the following to parse that text field and output the correct HTML formatting, but I think it is pretty brute force-- not very much finesse.
$cv = get('cv_text');
$cv = strip_tags($cv);
// Remove empty line breaks
$cv = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "", $cv);
// Explode line breaks
$cv = explode("\n", $cv);
$i = 0;
foreach($cv as $line) {
if( preg_match('/\[section \"(.*)\"\]/', $line) ) {
echo preg_replace('/\[section \"(.*)\"\]/', '<h2>$1</h2>', $line);
} elseif( preg_match('/\[year (\d*)\]/', $line) ) {
echo preg_replace('/\[year (\d*)\]/', '<h3>$1</h3>', $line);
} else {
// Count every two lines and wrap in a <p> tag
if($i === 1) {
echo '<em>'.$line.'</em></p>';
$i = 0;
continue;
} else {
echo '<p>'.$line.'</br>
';
$i++;
}
}
}
Can anyone recommend a way to improve my present code or present a better approach?
Thanks!