Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Ok. So on this page I have the data being pulled in using the SourceQuery class but when I get the hostname there is extra data that I don't want on the page.

This is the page: http://almost-there.org/beta.php

This is the code that I am using.

$SName = $StatsTF["HostName"];
$SName = str_replace("Almost There | ", "", $SName);

The full string that it returns is: "Almost-There | Stock Maps #1 | East Coast (x24)"

The end result I am trying to get to is: "Stock Maps #1 | East Coast (x24)"

Am I right in using str_replace or should I be using another function?
If I should be str_replace then what am I doing wrong?

share|improve this question
This site is a place for you to get feedback on your working code. This is not a place for "How do I do X in Y?" or "What am I doing wrong?" questions. – sepp2k Jan 2 at 15:28

closed as off topic by Brian Reichle, Corbin, sepp2k Jan 2 at 15:30

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

3 Answers

You are trying to replace the string "Almost There" when you should be trying to replace the string "Almost-There". Seems you are missing the dash between the words.

Edit: Looking at the hostname on the page, it seems that the original hostname doesn't even have a dash between the two words, so I don't know what's going on there. Alternatively you could try using substr on the hostname variable to cut off the correct number of characters you don't require. Something like:

$SName = substr($SName, 15);
share|improve this answer

This is a slight improvement:

$SName = str_replace('Almost-There | ', '', $StatsTF['HostName']);
  1. Double quotes "", PHP parses the string for variables, single quotes '', PHP doesn't parse the string.
  2. There's no reason to store the variable $SName and then process it. Just get and process it in one fell swoop.
share|improve this answer

Looking at the code, I can't see anything that would lead to str_replace not working. The only thing I can see that might trip it up would be the pipe (|). It might be using the pipe as a preg match; try using single quotes instead of double:

$SName = str_replace('Almost There | ', '', $SName);

Or escaping the pipe, "Almost There \| " ...

I could be wrong on both accounts (my PHP is about a year old :/) but you're using str_replace correctly.

share|improve this answer
Correct me if I am wrong but The string: "Almost-There" is different from the string: "Almost There" – Phill Fernandes Nov 25 '12 at 23:20
2  
Yup, you are correct, we both must have been high when we read your code .. – txtechhelp Nov 27 '12 at 18:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.