Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
// Overview: Find the regular expr. (preg_replace_callback) => func. "articles" => $match1=topic; $match2=author; $realMonth=month; $print=article summary
$text=$_GET['text'];
function articles($matches){
// $print contains article summary
if (strlen($matches[4])>100){
$str=substr((trim($matches[4])),0,100);
$print=htmlspecialchars($str)."...";
}else{
$print=htmlspecialchars(trim($matches[4]))."...";
}
//$month contains month number as str
$month=substr(($matches[3]),3,2);
// switch-case for the month's name
switch ($month){
case "01":
$realMonth="January";
break;
case "02":
$realMonth="February";
break;
case "03":
$realMonth="March";
break;
case "04":
$realMonth="April";
break;
case "05":
$realMonth="May";
break;
case "06":
$realMonth="June";
break;
case "07":
$realMonth="July";
break;
case "08":
$realMonth="August";
break;
case "09":
$realMonth="September";
break;
case "10":
$realMonth="October";
break;
case"11":
$realMonth="November";
break;
case "12":
$realMonth="December";
break;
}
//$match1 - topic
$match1=htmlspecialchars(trim($matches[1]));
//$match2 - author
$match2=htmlspecialchars(trim($matches[2]));
echo "<div>"."\n"."<b>Topic:</b> <span>$match1</span>\n"."<b>Author:</b> <span>$match2</span>\n"."<b>When:</b> <span>$realMonth</span>\n"."<b>Summary:</b> <span>$print</span>\n</div>\n";
}
//every time when finds the regular expr. will perform "article" function above
preg_replace_callback(
'/(.+?)[%](.+?)[ ;](\d{2}-\d{2}-\d{4})[ -]((.+?)*)/',"articles",$text
);?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.