...

View Full Version : Basic Text Class



Zangeel
11-16-2008, 07:04 PM
This can help jumpstart you creating text validations.. parses urls, and has some bbcode.. work off it.



<?php

class TextClass
{

public function Parse($sText)
{

if (!function_exists(stripos)) {
function stripos($string, $word)
{
$retval = false;
for ($i = 0; $i <= strlen($string); $i++) {
if (strtolower(substr($string, $i, strlen($word))) == strtolower($word)) {
$retval = true;
}
}
return $retval;
}
}


if (stripos($sText, "a href") === false) {

if (stripos($sText, "img") === false) {

if (stripos($sText, "[/link]") === false) {

if (stripos($sText, "src=") === false) {

if (stripos($sText, "[/url]") === false) {


$sText = " " . $sText;
$sText = eregi_replace('(((f|ht){1}tps?://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
'<a href="\\1" target=\"_blank\">\\1</a>', $sText);

$sText = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '\\1<a href="http://\\2" target=_blank>\\2</a>',
$sText);


$sText = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})',
'<a href="mailto:\\1">\\1</a>', $sText);

}
}
}
}
}



$Arr = array(
"/\(.+?)\[\/b\]/si",
"/\[i\](.+?)\[\/i\]/si",
"/\[quote=(.+?)\](.+?)\[\/quote\]/si",
"/\[quote\](.+?)\[\/quote\]/si",
"/\[url=(.+?)\](.+?)\[\/url\]/si"
);

$Rep = array(
"<b>$1</b>",
"<i>$1></i>",
"<blockquote>$1 <b>Said</b> <br /> $2</blockquote>",
"<blockquote>$1</blockquote>",
"<a href=\"$1\" target=\"_blank\">$2</a>"
);

$sText = preg_replace ($Arr, $Rep, $sText);

return $sText;

}

}


example usage



require ('Text.class.php');

$oTxt = New TextClass;

$Str = "This is a test [b]string to test the text class out. <br><br>
hello there how are you test test test test test test test test test test test test test test test <br><br><br> www.google.com ... http://www.google.com ...emai@eamil.com<BR><BR><BR>";

echo $oTxt->Parse($Str);

Inigoesdr
11-16-2008, 07:34 PM
Thank you for your post.
A couple of things to look at:




require ('Text.class.php') or die("Error");
That "or die" section isn't needed, and breaks the script.

You should use preg_*(PCRE) vs. ereg_*(POSIX) regex functions because preg_* is faster, and the ereg_* functions are going to be removed soon anyway.

Also, you have a return after a return in your method... which doesn't make any sense.. the execution will stop when it reaches the first return.

Zangeel
11-17-2008, 05:04 PM
yea fixed the return thing, meant to have $sText :P
thanks



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum