Page 1 of 1

String Replace In An Array

Posted: Mon Oct 25, 2004 6:48 am
by ajfton
Hey ALL - New member first time poster here , :-)

I have a PHP script that I would like to modify, unfortunatly, my stong point in servers, hardware, *nix, etc.. not programming. I do have a basic understanding and can usually make small changes and customizations.

I have a script (see code below) that created a 1 page site from a templatate taht is on the server. It take the information the person enters into a form and them proccesses it - put the data from the form into the HTML template in between "comment tags" for each of the fields.

What I Want To Do:

I want to be able able to do some kind of "string replce" on the array BEFORE it processes the HTML page.

I would the user to be able to put a few basic HTML commands into the form, problem is my users do not know them, So instead if a user could put "NEWLINE" in the form, the script would reaplce that with "<BR />" , hopefully this makes sence.

I only want a few things, like a new line, to be able to bold some text, and maybe Ittalics.

Any help would very nice

Code: Select all

function make_page($sTemplatePath, $pageBase, $sNewFileName, $fieldArray)&#123;
	
// $sTemplatePath: the relative path to the template that we parse
// $pageBase: the relative path to the base dir.
// $sNewFileName: the name for the created file after template parsing
	
	global $Member_ID;

while(list($key,$val) = each($fieldArray)) &#123;   
     $fieldArray&#1111;$key] = stripslashes($val);  
&#125;  
reset($fieldArray);

	if(!isset($fieldArray&#1111;'img1']))&#123;
		$fieldArray&#1111;'form_email'] = "<input type="hidden" name="recipient" value="&#123;$fieldArray&#1111;'owner_email']&#125;">";
		$fieldArray&#1111;'form_id'] = "<input type="hidden" name="Member_ID" value="$Member_ID">";
	&#125;

// Get The Template HTML & Combine Into A String

	$template = file($sTemplatePath);
	$template = implode('', $template);

// Insert Data Between The Comment Tags

	while(list($sFieldName, $value) = each($fieldArray))&#123;
		$sReplaceString = "<!--field $sFieldName-->.*<!--end $sFieldName-->";
		$sReplaceWith = "<!--field $sFieldName-->$value<!--end $sFieldName-->";
		$template = ereg_replace($sReplaceString, $sReplaceWith, $template);
	&#125;

//If The Folder Doesnt Already Exist, Create It.

	if (!(@opendir("$pageBase/$Member_ID")))&#123;
		mkdir("$pageBase/$Member_ID", 0777);
		chmod("$pageBase/$Member_ID", 0777);
	&#125;
		
	if (!(@opendir("$pageBase/$Member_ID/&#123;$fieldArray&#1111;'address']&#125;")))&#123;
		mkdir("$pageBase/$Member_ID/&#123;$fieldArray&#1111;'address']&#125;", 0777);
		chmod("$pageBase/$Member_ID/&#123;$fieldArray&#1111;'address']&#125;", 0777);
	&#125;
		
	$file = fopen("$pageBase/$Member_ID/&#123;$fieldArray&#1111;'address']&#125;/$sNewFileName", 'w');
	fputs($file, $template);
	fclose($file);
	chmod("$pageBase/$Member_ID/&#123;$fieldArray&#1111;'address']&#125;/$sNewFileName", 0777);

&#125;
Also, if I am totally wrong on the string replace thing here and there is another way of doing it, please let me know.


AJ

Also...

Posted: Mon Oct 25, 2004 6:52 am
by ajfton
Sorry for posting again but I forgot something..

Does anyone know how to do some kind of "string replace" on all links that are entered into the form - so they would be converted to clickable HTML links on the page that is created.

Is there any way to take "http://?????" and replace it with:

Code: Select all

<a href="http://?????">http://?????</a>

Posted: Mon Oct 25, 2004 6:59 am
by kettle_drum
You can simply use str_replace() to change what you want after the array has been imploded into a string:

Code: Select all

$template = implode('', $template); 

$template = str_replace('this', 'with this', $template);
And you can use regex to search for links and turn them into links. Check the ereg() section in the php manual.