Category Archives: Easy PHP

How to explode a string and create hyperlink?

Here is very easy code to create Tagged List with hyperlink.
I have found several events where we have to create dynamic URL with Tagged text.
I have used very simple approach. Just Exploded sting and replaced space with “-”
You can use PHP function preg_replace() or str_replace() then used here-docs for creating hyperlink.
Instead of Echo Hear Docs is much better solution.
And at last removed the last Comma, and return your comma separated Dynamic links.

$tags = "jQuery Tutorials, PHP Display Errors, Drupal Module";
$tagL="";
function b2b_article_LinkedTags($stringTags) {
	$expTags = explode(",",$stringTags);
	foreach($expTags as $key=>$Tagevalue) {
	$Tagevalue = trim($Tagevalue);
	$link = preg_replace('/[^-a-z0-9]/', '-', strtolower($Tagevalue) );
	//$link = str_replace(" ","-", strtolower($Tagevalue));
	$tagL.=<<$Tagevalue,
EOF;
	}
	
	return substr($tagL, 0, -1);
}  
echo b2b_article_LinkedTags($tags)

Share

How to display certain number of words from a record?

I got good php tutorial to display certain number of words from database; in place of Database record you can use any other regular string.

 300) {
$ext = "... read more";
} else {
$ext = "";
}
function elliStr($s,$n) {
for ( $x = 0; $x < strlen($s); $x++ ) {
$o = ($n+$x >= strlen($s)? $s : ($s{$n+$x} == " "?
substr($s,0,$n+$x) . "..." : ""));
if ( $o!= "" ) { return $o; }
}
}

echo (elliStr("$text", 300)) . $ext;
?>
Share