Jump to content

PHP Wiki Like Function


eli311

Recommended Posts

Hi,

 

So im trying to make a php function that turns [[img:testimage.png/Img]] to and url to the image.

 

e.g.

from: [[img:testimage.png/Img]]

To: http://www.mytestsite.com/images/testimage.png

 

Here is the string to change: Change this to an URL [[img:testimage.png/Img]] And this one [[img:testimage1.png/Img]]

 

Here is the PHP Function:

$string="Change this to an URL [[img:testimage.png/Img]] And this one [[img:testimage1.png/Img]]";

function findImage($string){
/* The Var's */
$Image_found 		= false;
$ImageName 		= array();
$mystring			= $string;
$Image_Start   	= '[[img:';
$Image_End   		= '/Img]]';
/* The Var's */
/* Find if the Img tags are there */
$ImageCount1 		= substr_count($mystring, $Image_Start);
$ImageCount2		= substr_count($mystring, $Image_End);
/* Find if the Img tags are there */
/* Find out where they are */
$WhereImage_Start 	= strpos($mystring, $Image_Start);
$WhereImage_End 	= strpos($mystring, $Image_End);
/* Find out where they are */
/* If the counts equal the same continue */
if($ImageCount1 == $ImageCount2){
if ($WhereImage_Start !== false && $WhereImage_End !== false) {
	$Image_found = true;
}
/* Start the loop */
if($ImageCount1 >= 1 && $Image_found == true){
for($i=0;$i<$ImageCount1;$i++){
	$Find_image = substr($mystring, $WhereImage_Start+6, ($WhereImage_End-6)-$WhereImage_Start);
	$replace = IMG_LINK."/$Find_image";
	$search = "$Image_Start$Find_image$Image_End";
	$mystring = str_replace($search, $replace, $mystring);
}
/* End the loop */
/* Return new string */
echo $mystring."<br/></br>";
}
}else{
/* Error Return start string*/
echo $mystring;	
}
}

findImage($string);

 

This is what the function returns:

Change this to an URL http://www.mytestsite.com/images/testimage.png And this one [[img:testimage1.png/Img]]

 

Now this is not right it should have done both of them, but i can not get it to do more then one.

 

Please can someone explain and help me to get it to do as many Img tags as there are.

 

Thanks for any help

Eli Stone

Link to comment
https://forums.phpfreaks.com/topic/205261-php-wiki-like-function/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.