pyrodude Posted August 4, 2007 Share Posted August 4, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/63292-solved-substr_replace/ Share on other sites More sharing options...
Barand Posted August 4, 2007 Share Posted August 4, 2007 Side note: I don't know the limit, but largest text file I have on my pc currently is within it. <?php $txt = file_get_contents('data/mydata.cif'); echo strlen($txt); // --> 180270359 ?> Quote Link to comment https://forums.phpfreaks.com/topic/63292-solved-substr_replace/#findComment-315432 Share on other sites More sharing options...
Barand Posted August 4, 2007 Share Posted August 4, 2007 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] Quote Link to comment https://forums.phpfreaks.com/topic/63292-solved-substr_replace/#findComment-315434 Share on other sites More sharing options...
pyrodude Posted August 4, 2007 Author Share Posted August 4, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/63292-solved-substr_replace/#findComment-315529 Share on other sites More sharing options...
pyrodude Posted August 4, 2007 Author Share Posted August 4, 2007 Would a preg_replace() be more efficient than trying to use substr_replace()? Quote Link to comment https://forums.phpfreaks.com/topic/63292-solved-substr_replace/#findComment-315544 Share on other sites More sharing options...
dbo Posted August 4, 2007 Share Posted August 4, 2007 preg_replace is much more robust/powerful but slower to run... assuming you mean efficient for the computer. Quote Link to comment https://forums.phpfreaks.com/topic/63292-solved-substr_replace/#findComment-315570 Share on other sites More sharing options...
Barand Posted August 4, 2007 Share Posted August 4, 2007 You are right about the =>. I've also tried the code with a mix of <?php and <? tags successfully. Quote Link to comment https://forums.phpfreaks.com/topic/63292-solved-substr_replace/#findComment-315602 Share on other sites More sharing options...
pyrodude Posted August 4, 2007 Author Share Posted August 4, 2007 Well it must work then! Thanks so much! Quote Link to comment https://forums.phpfreaks.com/topic/63292-solved-substr_replace/#findComment-315604 Share on other sites More sharing options...
pyrodude Posted August 4, 2007 Author Share Posted August 4, 2007 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'? Quote Link to comment https://forums.phpfreaks.com/topic/63292-solved-substr_replace/#findComment-315628 Share on other sites More sharing options...
Barand Posted August 4, 2007 Share Posted August 4, 2007 The code isn't using the from and to params, it's using the array version. Try running it - it isn't lethal Quote Link to comment https://forums.phpfreaks.com/topic/63292-solved-substr_replace/#findComment-315640 Share on other sites More sharing options...
pyrodude Posted August 4, 2007 Author Share Posted August 4, 2007 Lol, thanks. I'm actually at work right now, and don't have access to my server. But I fully intend to investigate when I get home! Quote Link to comment https://forums.phpfreaks.com/topic/63292-solved-substr_replace/#findComment-315642 Share on other sites More sharing options...
Barand Posted August 4, 2007 Share Posted August 4, 2007 Tut tut - browsing the freaks forum and w3schools when you should be working Quote Link to comment https://forums.phpfreaks.com/topic/63292-solved-substr_replace/#findComment-315644 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.