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
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
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
Share on other sites

So, I was reading up on strtr() and w3schools.com says

Note: If from and to are different length, both will be formatted to the length of the shortest.

 

Does this mean that the <span ...> code will be truncated to the length of '<?php'?

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.