Jump to content

Replacing Characters with Divs


jwaldrip

Recommended Posts

I am trying to replace characters with HTML. I need to look at a letter within a string and replace it with a div. For example in "jason" this would return 5 divs each with their own class. Glance at my code, what am i doing wrong?

 

 

<?

$repdiv[0] = '<div class="a"></div>'; 
$repdiv[1] = '<div class="b"></div>'; 
$repdiv[2] = '<div class="c"></div>'; 
$repdiv[3] = '<div class="d"></div>'; 
$repdiv[4] = '<div class="e"></div>'; 
$repdiv[5] = '<div class="f"></div>'; 
$repdiv[6] = '<div class="g"></div>'; 
$repdiv[7] = '<div class="h"></div>'; 
$repdiv[8] = '<div class="i"></div>'; 
$repdiv[9] = '<div class="j"></div>'; 
$repdiv[10] = '<div class="k"></div>'; 
$repdiv[11] = '<div class="l"></div>'; 
$repdiv[12] = '<div class="m"></div>'; 
$repdiv[13] = '<div class="n"></div>'; 
$repdiv[14] = '<div class="o"></div>'; 
$repdiv[15] = '<div class="p"></div>'; 
$repdiv[16] = '<div class="q"></div>'; 
$repdiv[17] = '<div class="r"></div>'; 
$repdiv[18] = '<div class="s"></div>'; 
$repdiv[19] = '<div class="t"></div>'; 
$repdiv[20] = '<div class="u"></div>'; 
$repdiv[21] = '<div class="v"></div>'; 
$repdiv[22] = '<div class="w"></div>'; 
$repdiv[23] = '<div class="x"></div>'; 
$repdiv[24] = '<div class="y"></div>'; 
$repdiv[25] = '<div class="z"></div>';

$letters[0] = '/a/';
$letters[1] = '/b/';
$letters[2] = '/c/';
$letters[3] = '/d/';
$letters[4] = '/e/';
$letters[5] = '/f/';
$letters[6] = '/g/';
$letters[7] = '/h/';
$letters[8] = '/i/';
$letters[9] = '/j/';
$letters[10] = '/k/';
$letters[11] = '/l/';
$letters[12] = '/m/';
$letters[13] = '/n/';
$letters[14] = '/o/';
$letters[15] = '/p/';
$letters[16] = '/q/';
$letters[17] = '/r/';
$letters[18] = '/s/';
$letters[19] = '/t/';
$letters[20] = '/u/';
$letters[21] = '/v/';
$letters[22] = '/w/';
$letters[23] = '/x/';
$letters[24] = '/y/';
$letters[25] = '/z/';

ksort ($letters);
ksort ($repdiv);

$printname = preg_replace( $letters , $repdiv, 'teststring' ); 
echo $printname;

?>

Link to comment
Share on other sites

$string = "teststring";

for($i=0;$i<strlen($string);$i++){
echo "<div class=\"".$string[$i]."\"></div>\n";
}

 

Worked for me.

 

Function wise could be:

 

function convert2div($string){
for($i=0;$i<strlen($string);$i++){
	$text .= "<div class=\"".$string[$i]."\"></div>\n";
}

return $text;
}

echo convert2div('monkeys');

Link to comment
Share on other sites

Glance at my code, what am i doing wrong?

I think (but not sure) its because:

 

Lets say your test string is 'ab'.

 

After the first match "a" is replaced the string has now changes to '<div class="a"></div>b'.

 

Now when it looks the next matches to replace it will the letters in teh div/class tag and replace them too.

 

View the html source of the output to see all the mangled divs.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.