Forum Moderators: coopster

Message Too Old, No Replies

how to streamline scripting for update

         

fwordboy

5:28 pm on Jan 23, 2005 (gmt 0)

10+ Year Member



I have some php which converts plain text, from an input form, into valid HTML. This code is on several pages and I'd like to be able update the code more easily than going into each page each time and edit every snippet of code. I think, but not sure, that I need to create a global function and include all the code in a separate file but, I don't know how.

my code:


$description = $_POST['description'];
$description = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\">\\0</a>", $description); // turn text links into <a href> links
$description = preg_replace("/\r/", "<br />",$description); // turn line breaks into xhtml br tags
$description = preg_replace("¦<br />\n<br />¦","</p>\n<p>",$description); // turn carriage return-line feeds into xhtml p tags
$description = str_replace("&","&amp;",$description); // turn & into escaped ampersands
$description = str_replace("£","&pound;",$description); // turn £ into escaped pounds

I think textilePHP and PHP markdown do something similar whereby you just include the code file and for every time the code is needed use

Textile(this)
.

In my example I have used

$description = $_POST['foo']
but sometimes $description may need to be derived from e.g. $_POST['bar']

mcibor

8:35 pm on Jan 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



file function.php:

if(function_exists('my_replace'))
{
function my_replace($description)
{
$description = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\">\\0</a>", $description); // turn text links into <a href> links
$description = preg_replace("/\r/", "<br />",$description); // turn line breaks into xhtml br tags
$description = preg_replace("¦<br />\n<br />¦","</p>\n<p>",$description); // turn carriage return-line feeds into xhtml p tags
$description = str_replace("&","&amp;",$description); // turn & into escaped ampersands
$description = str_replace("£","&pound;",$description); // turn £ into escaped pounds

return $description;
}
}

in your php code:

include("function.php");//include the file with the function

$description = my_replace($_POST['description']); //call the function

It's elementary my dear Watson...:)

Hope it helps you with the functions

fwordboy

1:26 pm on Jan 25, 2005 (gmt 0)

10+ Year Member



thanks alot for the info, but I'm gettin the following error:

Fatal error: Call to undefined function my_replace()

I have defo included the file properly as I've put a little echo stetment in the top saying 'i have been included' that is showing up.

jusdrum

5:19 pm on Jan 25, 2005 (gmt 0)

10+ Year Member



One tip that might help: preg_replace will take arrays for the first two arguments. So you can do this:

$description = $_POST['description'];
$Searches = array("/\r/","¦<br />\n<br />¦");
$Replacements = array("<br />","</p>\n<p>");
preg_replace($Searches,$Replacements,$description);

fwordboy

9:43 pm on Jan 25, 2005 (gmt 0)

10+ Year Member



thanks jusdrum, that's cutting the code down a bit but, I'm still getting the error when I try to run it as a function. Any ideas why this is?

jusdrum

12:23 am on Jan 26, 2005 (gmt 0)

10+ Year Member



if(function_exists('my_replace'))

I think this may be the problem, it's only defining the function if it exists, so basically your function isn't being defined at all! The correct way should be defining the function if it *doesn't* exist, like so:

if (!function_exists('my_replace'))

So by just adding that exclamation, it should fix your problem.

fwordboy

11:01 am on Jan 26, 2005 (gmt 0)

10+ Year Member



thats the trick. thanks to both mcibor and jusdrum for all your help.

mcibor

2:58 pm on Jan 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry for the! error. I keep getting it myself.

Best wishes!
Michal Cibor