Jump to content

[SOLVED] substr_replace()


pyrodude

Recommended Posts

I'm integrating a javascript online WYSIWYG, but it has no handling for php code or tags.  What I was planning to do was change

<?php

to

<font color=orange>[code=php:0]

and

<?

to


</font>[/code]

to make it a lot easier to pick out.  I can get the

[code=php:0]

and


[/code]

to substitute easy enough on read and write, but when I added the font tag (I know, it's deprecated...was just in a rush) I get a weird display.

 

substr_replace() is turning

<?php

into:


ont color=orange>

 color=orange>[code=php:0]

 

My function to replace the tags is as follows:

function phpTagsReplace($editfile) {
    $file = file_get_contents($editfile);
    $count = substr_count($file,'<?php');
    if ($count !== 0) {
        for ($i = 0; $i<=$count; $i++) {
            $strpos = strpos($file,'<?php');
            $file = substr_replace($file,'<font color=orange>[code=php:0]',$strpos,5);
        }
    }
    $count = substr_count($file,'<?');
    if ($count !== 0) {
        for ($i = 0; $i<=$count; $i++) {
            $strpos = strpos($file,'<?');
            $file = substr_replace($file,'<font color=orange>[code=php:0]',$strpos,2);
        }
    }
    $count = substr_count($file,'?>');
    if ($count !== 0) {
        for ($i = 0; $i<=$count; $i++) {
            $strpos = strpos($file,'?>');
            $file = substr_replace($file,'

</font>',$strpos,2);

        }

    }

    return $file;

}

[/code]

 

On a side note, how big of strings can file_get_contents() and file_put_contents handle?  Should I think about switching to fopen() and fread()/fwrite()?

 

Thanks for the help!

Link to comment
https://forums.phpfreaks.com/topic/63292-solved-substr_replace/
Share on other sites

Using a file containing just opening and closing php tags,

<?php

?>

 

the function actually returns this (view the html source)

 


</font>ont color=orange>

 color=orange>[code=php:0]

</font>[/code]

 

 

try this alternative (slightly renamed) function

<?php
function phpReplaceTags($editfile)
{
    $srch = array (
                '<?php' => '<span style="color:orange">[code=php:0]',
                '<?' => '<span style="color:orange">[code=php:0]',
                '?>' => '

</span>'

            ) ;

    $file = file_get_contents($editfile);

    return strtr($file, $srch);

}

echo '<pre>', phpReplaceTags('noname2.php'), '</pre>';

?>

[/code]

Link to comment
https://forums.phpfreaks.com/topic/63292-solved-substr_replace/#findComment-315434
Share on other sites

I haven't tried your code yet, but I have a question - I'm fairly new to php coding and have no idea what the => operator does, but my guess would be that it assigns the value to the right of the symbol to the array $srch with the key of the value to the left of the symbol.  Is that correct?  Also, will it do all of the <?php replaces prior to trying to do <? replaces?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/63292-solved-substr_replace/#findComment-315529
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.