Is it possible to add a custom keyword after a URL

Discussion in 'PHP' started by allby, Oct 20, 2010.

  1. #1
    Hi guys,
    this is my problem.
    I use wordpress and i have diffrent titles for every post. The php code for the title is
    <?php the_title(); ?>
    PHP:
    I want to add my title after this url: http://search.msn.com/results.aspx?format=rss&FORM=RSRE&q=
    That means if i have a post with this title: php tutorials
    The url should be like this: http://search.msn.com/results.aspx?format=rss&FORM=RSRE&q=php+tutorials

    This is the part of my php code that contains the url:
    
    <?php
    
       $url="http://search.msn.com/results.aspx?format=rss&FORM=RSRE&q=php+tutorials";
    
       $rss=new rssFeed($url);
    PHP:
    I tried this already but it is not working:
    
    <?php
    
       $url="http://search.msn.com/results.aspx?format=rss&FORM=RSRE&q=<?php the_title(); ?>";
    
       $rss=new rssFeed($url);
    PHP:
    Sorry, i hope you understand what i mean.
     
    allby, Oct 20, 2010 IP
  2. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #2
    You can not have nested PHP tag. Whenever you start a PHP tag, you must close it (except if you are treating it as a literal):

    
    <?php
    
       $url="http://search.msn.com/results.aspx?format=rss&FORM=RSRE&q=".the_title();
    
       $rss=new rssFeed($url);
    
    PHP:
    The above should work assuming that the_title function returns the title and not print it out.
     
    ThePHPMaster, Oct 20, 2010 IP
  3. allby

    allby Greenhorn

    Messages:
    94
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    18
    #3
    Thanks PHPMaster for your reply.
    I tried what you said. But it is not working. Is there any other solution? I also want to replace "spaces" with "+". That means if i have php tutorials as title the output should be like this: php+tutorials
     
    allby, Oct 20, 2010 IP
  4. max2010

    max2010 Greenhorn

    Messages:
    81
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #4
    try:

    $title=urlencode($post->post_title);
    $url="http://search.msn.com/results.aspx?format=rss&FORM=RSRE&q=".$title;
     
    max2010, Oct 21, 2010 IP
  5. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #5
    Sorry for the late reply.

    First step, is you need to open the function the_title() (search for it).

    To be on the safe side (in the event that other places are using it). Change the function declaration to:

    
    
    // Change
    function the_title()
    
    // To
    function the_title($returnVar = false)
    
    PHP:
    Now look at the function itself, look for any echo's/print's and replace them with return if $returnVar is true. For example:

    
    // Change
    echo 'Site title';
    
    // To
    if($returnVar){ return 'Site title';}else { echo 'Site title';}
    
    PHP:
    Now your code will look like:

    
      $title = str_replace(' ','+',the_title(true));
    
       $url='http://search.msn.com/results.aspx?format=rss&FORM=RSRE&q='.$title;
    
       $rss=new rssFeed($url)
    
    PHP:
    Hope this helps.
     
    ThePHPMaster, Nov 7, 2010 IP