Help With str_replace()

I have a php scraper that I wrote that scrapes <td class="title>job title tags from a job hunting website and stores it in a PHP variable. The problem is that the href is a relative link (just folder directories) and when I use it on my site it appends my domain name instead of the job hosting site’s domain name.

I tried to use str_replace() but I can’t seem to get it to do anything. Here’s my snippet of PHP

[PHP] //scraping job title and url to job listing
//appends brando56894.dyndns-blog.com to url instead of http://huntersandgatherers.jobamatic.com/
$regex_title= “/<td class=“title”>(.+?)</td>/”;
preg_match_all($regex_title,$scraped_page,$scraped_title_data);
for($k=0;$k<5;$k++) { $job_title = $scraped_title_data[1][$k];}

//doesn’t work
$brando = urlencode(“http://brando56894.dyndns-blog.com:1085”);
$hunter = urlencode(“http://huntersandgatherers.jobamatic.com”);
$job_title = str_replace($brando, $hunter, $job_title);
//doesn’t work

echo $job_title;[/PHP]

The code seem not appends anything - links stays relative. It is how a web browser interpret relative links: it will just append path based on location of the HTML page with that relative link. So, what you need is just append your domain/path in front of this relative link:
[php]$job_title = str_replace(‘href="’,‘href="http://brando56894.dyndns-blog.com:1085/’,$job_title);[/php]

Also, this line not make sense:
[php]for($k=0;$k<5;$k++) { $job_title = $scraped_title_data[1][$k];}[/php]
Result of this code above is identical to this:
[php]$job_title = $scraped_title_data[1][5];[/php]

Thanks, but actually that for loop does make sense, at least to me. When the job titles get scraped (there’s actually 25 of them), they get stored into a multi-dimensional array and I need to extract them out one at a time so I can place them in info bubbles on a Google Map one at a time. If I don’t use a loop there’s no way that I know of to do it 25 times.

I just made this comment about the code what you posted here, and there was that line with loop. If you look closer at the mentioned loop, you have there 5 iterations total, and assigning value to a scalar variable - every next iteration overwrites previous value (thus loop not make sense). If, in your code you had an array element instead of scalar variable (like $job_title[]), then it definitely would make sense.

Using your code for str_replace I get Parse error: syntax error, unexpected T_VARIABLE.

I provided one line of code, with no syntax errors. Check the rest of your code, or, try to copy & paste the code from my post as is.

I did copy and paste your code, and when I comment out that line (which it specifically states in the error message, but I didn’t post it since it wasn’t relevant) it doesn’t throw the error message anymore.

Go ahead and post your entire code that gives you the error message. Once again, there is nothing specific in that line of code with str_replace() that would cause error.

Here’s all of my PHP code

[php] <?PHP
$scraped_page = file_get_contents(“http://huntersandgatherers.jobamatic.com/a/jobs/find-jobs/l-08901”);

  //scraping location
  $regex_location= '/<td class=\"location\">(.+?)<\/td>/';
  preg_match_all($regex_location,$scraped_page,$scraped_location_data);  
  for($i=0;$i<5;$i++){
    $location = $scraped_location_data[1][1];
    $url="http://maps.googleapis.com/maps/api/geocode/xml?address=$location&sensor=false";}

  //scraping company name
  $regex_company= '/<td class=\"company\">(.+?)<\/td>/';
  preg_match_all($regex_company,$scraped_page,$scraped_company_data);
  //for($j=0;$j<5;$j++){$company_name = $scraped_company_data[1][1];}
  $company_name = $scraped_company_data[1][1];

  //scraping job title and url to job listing appends brando56894.dyndns-blog.com to url instead of http://huntersandgatherers.jobamatic.com/
  $regex_title= "/<td class=\"title\">(.+?)<\/td>/";
  preg_match_all($regex_title,$scraped_page,$scraped_title_data);
  for($k=0;$k<5;$k++) {$job_title = $scraped_title_data[1][$k];}

  $job_title = str_replace('href="','href="http://brando56894.dyndns-blog.com:1085/',$job_title);
  echo $job_title;

  ?>[/php]

Just realized that some stuff (such as the for loops) may be commented out because I’ve been testing other things too. Everything works with my PHP code except for this problem with the URL.

I just tried your code, and it is working - no syntax errors. The problem is that link generated by this code is broken, because there is no such file on your website.

Yea I just uncommented it when I posted it and it didn’t throw any errors. I know the link doesn’t work because the files aren’t on my server, that’s the whole problem I’m having.

I want to replace my domain name with the domain name that is hosting the pages.

Sponsor our Newsletter | Privacy Policy | Terms of Service