error while trying to turn " " (spaces) into "-" dashes

Discussion in 'PHP' started by BrendanGall, Jan 9, 2009.

  1. #1
    what i am trying to do is take a title and strip out all the spaces and replace them with dashes. But i am getting the below error.

    Parse error: syntax error, unexpected T_STRING_CAST in /home/video/public_html/pages/games.php on line 492​

    Here is the code i have.

        <?
        function sef_url($stringa) {
    
       $allowed = 'abcdefghijklmnopqrstuvwxyz1324567890-';
    
       $stringa = strtolower(string);
    
       $temp = array();
       for($x = 0; $x < strlen($allowed); $x++) {
          $temp[] = $allowed[$x];
       }
       $allowed = $temp;
    
       $result = '';
       for($x = 0; $x < strlen($stringa); $x++) {
          if(in_array($stringa[$x], $allowed) > -1) {
             $result .= $stringa[$x];
          } else {
             $result .= '-';
          }
       }
    
       return $result;
       
    $url = sef_url($row[title]); 
    
    } ?>
    Code (markup):
    line 492 as the error mentions is this line:

    $stringa = strtolower(string);​

    The site is a gaming website in which there are pages for each individual game. I want to strip the spaces and put dashes to make the result usable in a url. Then i have a script that will pull rss feeds and i want it to pull news from various gaming sites using their tags and diplay links to the news items related to each individual game.

    the result will be used like this.

    cssfeed.addFeed("Joystiq", "http://www.joystiq.com/tag/<? echo $url; ?>/index.xml") //Specify "label" plus URL to RSS feed​


    any help on fixing this error and getting things working would be amazing! Thanks!
     
    BrendanGall, Jan 9, 2009 IP
  2. Black Nova

    Black Nova Banned

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    to do that you'll need to probably use echo ' '; to make it work.
     
    Black Nova, Jan 9, 2009 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    Would a simple regex be an easier way to do this?

    $new_string = preg_replace('/\s+/', '-', $string);
     
    jestep, Jan 9, 2009 IP
  4. harrisunderwork

    harrisunderwork Well-Known Member

    Messages:
    1,005
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    135
    #4
    This code will replace multiple blank space with single dash.

    You can try this if u want 1:1 relationship

    $new_string =str_replace(' ','-',$string);
    Code (markup):
    Thanks
     
    harrisunderwork, Jan 9, 2009 IP
  5. Idle Play

    Idle Play Notable Member

    Messages:
    2,787
    Likes Received:
    128
    Best Answers:
    0
    Trophy Points:
    245
    #5
    I agree that the above str_replace would be a much better option. The original code goes round the houses for a fairly straight forward task.

    One thing I did notice is that $stringa = strtolower(string); - should be $stringa = strtolower($stringa); I assume.
     
    Idle Play, Jan 9, 2009 IP
  6. Black Nova

    Black Nova Banned

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yeah you guys are right.
     
    Black Nova, Jan 9, 2009 IP
  7. BrendanGall

    BrendanGall Peon

    Messages:
    225
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I dont know php enough to know exactly what that does.

    My goals are to make text lowercase as well as replace spaces with dashes. If you can come up with something for that and tell me how to use it, that would be amazing.


    I got the script from this website.
    http://www.total-php.com/article/6/generating-search-engine-friendly-titles-for-your-url/

    I changed the following code and it took the error away.

    One thing I did notice is that $stringa = strtolower(string); - should be $stringa = strtolower($stringa); I assume.​

    I just tried to echo the $url as just text on the page not including in the rss script, but nothing shows.
     
    BrendanGall, Jan 9, 2009 IP
  8. Idle Play

    Idle Play Notable Member

    Messages:
    2,787
    Likes Received:
    128
    Best Answers:
    0
    Trophy Points:
    245
    #8
    If all you're looking to do is substitute the space for a dash from the title pulled from the database (and make it lower case) just use :-

    $url = strtolower(str_replace(" ", "-", $row[title])); 
    Code (markup):
    ...and then echo the URL as you like.
     
    Idle Play, Jan 9, 2009 IP
  9. BrendanGall

    BrendanGall Peon

    Messages:
    225
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thank you very much Idle Play! It worked in doing what i asked.

    onto another problem though... The javascript is not displaying the feeds for whatever reason.

    It is inserting the text correctly though as i checked the source of the page.

    Thanks!
     
    BrendanGall, Jan 9, 2009 IP