Doing an str_replace on a function call? v.Wordpress

Discussion in 'PHP' started by mistergin, Feb 26, 2007.

  1. #1
    Howdy!

    Quick two-part question:

    1)

    In wordpress, the content of a blog post is held in the_content()

    I'd like to replace a string (ex: "###replaceme###") with a different one.

    I've tried a few variations like:
    str_replace("###replaceme###", "superlongstringwithintermixedphpandhtml", the_content);
    str_replace("###replaceme###", "superlongstringwithintermixedphpandhtml", the_content());
    str_replace("###replaceme###", "superlongstringwithintermixedphpandhtml", $the_content);

    etc.

    Any idea on how I'd do this? I believe I've also tried setting the function call to a local variable like ($blah = the_content();) and then replacing $blah but I don't recall that working either...



    2)

    In #1, I'm replacing that string with a div that has text, html, and php tags intermixed. In Cold Fusion there is a <cfsavecontent> tag that will allow you to basically set large blocks of code/text/etc to a variable name.

    Ex:

    <cfsavecontent variable="blah">
    <div>
    blah blah
    #some cool cf stuff here, etc#
    </div>
    </cfsavecontent>

    Is there anything similar in php? I've condensed the entirety of the "huge string" to a single line string in quotes, but I don't know if that will allow the php inside that string to process or not.



    Thanks in advance, if something is unclear, I'll be happy to rephrase. The body is willing, but the brain is reminding me that Heroes is on soon and I need to stop working :D :p
     
    mistergin, Feb 26, 2007 IP
  2. klown

    klown Peon

    Messages:
    2,093
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Do you want to replace all of the strings or just one?
     
    klown, Feb 26, 2007 IP
  3. mistergin

    mistergin Peon

    Messages:
    52
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    there's just one string I want to replace:

    ###replaceme###

    there's only one occurance per page, so one or all would be sufficiently effective.. that the answer you were looking for?
     
    mistergin, Feb 26, 2007 IP
  4. klown

    klown Peon

    Messages:
    2,093
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yeah.. actually on re-reading your question you seem to have more of a problem interacting with the text contained within the_content().. I wold work in solving that first If i was you. A simple str_replace should work if you have a variable holding the correct information.

    Why don't you just run the php processes before inputting the replacement text.. Here is an example of some code I used recently in a CMS that includes a glossary (I want to replace the first occurrence of a word with a link that has a popup of the definition on mouseover).

    
    	function str_replace_once($needle, $replace, $haystack) {
       // Looks for the first occurence of $needle in $haystack
       // and replaces it with $replace.
       $pos = strpos($haystack, $needle);
       if ($pos === false) {
           // Nothing found
           return $haystack;
       }
       return substr_replace($haystack, $replace, $pos, strlen($needle));
    } 
    
    		$x=0;
    		$max=count($glossary);
    		while ($x<$max)
    		{
    			$word=$glossary[$x]['word'];
    			$def=$glossary[$x]['definition'];
    			$used=$glossary[$x]['used'];
    			
    			if ($used=="no")
    			{
    				$replace="<a href=\"#\" class=\"statistics\" onmouseout=\"overlayclose('$word')\" onmouseover=\"return overlay(this, '$word', 'bottomright')\" onClick=\"return false;\">$word</a>";
    				$length=strlen($def);
    				$width=$length*8;
    				if ($width>200) {$width=200;}
    				$temp=strpos($text1,$word); 
    				if ($temp)
    				{
    					$text1=str_replace_once($word,$replace,$text1);
    					//echo "text1 used, $x <br>\n\n"; 
    					$glossary[$x]['used']="yes"; 
    					$divs.=" <DIV id=\"$word\" style=\"position:absolute; display:none; background-color: #FFFFFF; border-width: 1px; border-style: solid; border-color: #000000; width: $width; height: auto; padding: 1px\">$def</div>";
    				}
    			}
    			++$x;
    		}
    
    PHP:
    Now all you need to do is change the code a bit knocking off the loop.. also you should change the replace to fit your needs.
     
    klown, Feb 26, 2007 IP
  5. SilkySmooth

    SilkySmooth Well-Known Member

    Messages:
    1,583
    Likes Received:
    269
    Best Answers:
    0
    Trophy Points:
    180
    #5
    Hi,

    You cannot perform any action directly on 'the_content()' because the content is actually echo'd out within the function.

    You will need to edit the function directly, this is easy to do, just open up the 'includes/template-functions-post.php' file and locate the function, in my version it was on line 54 and looked like this:

    
    function the_content($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
    	$content = get_the_content($more_link_text, $stripteaser, $more_file);
    	$content = apply_filters('the_content', $content);
    	$content = str_replace(']]>', ']]&gt;', $content);
    	echo $content;
    }
    
    Code (markup):
    All you need to do then is add your str_replace before the echo call.

    As to your second question, you can concatenate your strings using the period, like so:

    
    $var="<div>";
    $var.="<b>test</b>";
    $var.="</div>";
    
    Code (markup):
    Note the period before the equals.

    HTH
     
    SilkySmooth, Feb 26, 2007 IP
  6. mistergin

    mistergin Peon

    Messages:
    52
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks guys, I found a function called apply_filter() that allowed me to get the replacement done successfully :)
     
    mistergin, Feb 27, 2007 IP