Search
Left Quote    Black holes are where God divided by zero.
- Steven Wright    
Right Quote
 
[login] | [Register]
Forum Index » PHP and mySQL » Viewing Topic and Replies
Post Reply


        Viewing Topic: str_replace problem
  This user is offline  sm0ke
  Subject: "str_replace problem" Posted: @ 1:13 am on Oct 01 2006   

Member #: 168
Rank: User - (83)
Since: 06/05/06
Posts: 34
From: Manchester - UK

I've got a problem with a str_replace() function for a site I'm working on, Its probably something and nothing but extra eyes are appreciated.

Im replacing spaces with underscores, and then & operatoers with the + symbol using the following code:


Code:

$query = "SELECT * FROM table WHERE t = 'stored' AND c = '1' ORDER BY i DESC LIMIT 0,6";
$r = mysql_query($qu ery) or die ("Error in query: $query" . mysql_error());

// if records present
if (mysql_num_rows ($r) > 0)
{

while($mx = mysql_fetch_obj ect($r))
{
$title = str_replace(' ', '_', $mx->l);
$title = str_replace('&a mp;', '+', $title);



Which successfully changes the characters as above.

To change them back again I'm using the following code:


Code:

$l = str_replace('_' , ' ', $l);
$l = str_replace('+' , '&', $l);
$sql = "SELECT * FROM table WHERE l = '$l'";
$provide = mysql_query($sq l) or die ("Error in query: $sql" . mysql_error());




Any title which does not contain the & sign resolves correctly with but the title containing "&&quo t; do not, the symbol is rewritten in the url but does not appear to convert back.

Any help

Cheers

Edited at 01:20:46 am on 10/01/06


Just because I dont care, doesnt mean I dont understand.
purepcs@hotmail
.co.uk |     Viewed: 12,862 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: str_replace problem" Posted: @ 2:49 am on Oct 01 2006    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

I think i have your answer

when you want to replace more than one item, use array()... like this:


Code:


$l = str_replace(arr ay('+', '&'), $l);

//and

$title = str_replace(arr ay('&', '+'), $title);




-bs0d | AllSyntax.com

    Viewed: 12,853 Times | Reply to This | To top
  This user is offline  ki
  Subject: "re: str_replace problem" Posted: @ 3:02 am on Oct 01 2006    

Member #: 204
Rank: User - (50)
Since: 09/16/06
Posts: 53
From: Florida

uh, try this

Code:

$da = array('+');
$l = str_replace($da ,'&',$l)



by the way what font this site use?



Edited at 03:12:03 am on 10/01/06


woohoo!

    Viewed: 12,850 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: str_replace problem" Posted: @ 3:20 am on Oct 01 2006    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

This site or Smoke's? I use: font-family: Verdana, Tahoma;




-bs0d | AllSyntax.com

    Viewed: 12,843 Times | Reply to This | To top
  This user is offline  ki
  Subject: "re: str_replace problem" Posted: @ 4:18 am on Oct 01 2006    

Member #: 204
Rank: User - (50)
Since: 09/16/06
Posts: 53
From: Florida

  bs0d said...

< i> This site or Smoke's? I use: font-family: Verdana, Tahoma;
I did mean this site and, I use the same thing




woohoo!

    Viewed: 12,841 Times | Reply to This | To top
  This user is offline  sm0ke
  Subject: "re: str_replace problem" Posted: @ 11:11 am on Oct 01 2006    

Member #: 168
Rank: User - (83)
Since: 06/05/06
Posts: 34
From: Manchester - UK

Ah

My bad im afraid. Both ways posted above and the way I was working it all worked fine, but I was trying to pass operators like "+" and "&&q uot; via a url, making the site think that halfway thru a title it had a new variable to process and therefore not matching a full title so returning NULL.

As opposed to writing a function to change the dynamic url as well as the rewritten url, I've changed all 35 whole records containing "&&q uot; to either "and&quo t; or "n" and will advise the site admin to do so.

I'll probably fix it up on the next release of their site and post a solution here.

Thanks for the help




Just because I dont care, doesnt mean I dont understand.

purepcs@hotmail
.co.uk |     Viewed: 12,834 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: str_replace problem" Posted: @ 2:59 pm on Oct 01 2006    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

Have you tried &amp; for "&&q uot; and &#43; for "+" ? put the codes in your url and they will show up as & and +




-bs0d | AllSyntax.com

    Viewed: 12,831 Times | Reply to This | To top
  This user is offline  sm0ke
  Subject: "re: str_replace problem" Posted: @ 4:28 pm on Oct 01 2006    

Member #: 168
Rank: User - (83)
Since: 06/05/06
Posts: 34
From: Manchester - UK

Tried that but it was still the same.

I think its due to the php script grabbing a url like - detail.php?var= this & that

so the browser is reading it as

detail.php?var= this & that=(no var)

and only matches the part prior to & in the database, as replacing the actual titles on pages works regardless of how the str_replace() is constructed.

cheers




Just because I dont care, doesnt mean I dont understand.

purepcs@hotmail
.co.uk |     Viewed: 12,826 Times | Reply to This | To top
  This user is offline  misterhaan
  Subject: "re: str_replace problem" Posted: @ 2:38 pm on Oct 02 2006    

Member #: 5
Rank: User - (213)
Since: 02/11/05
Posts: 148
From: chair

you need to url encode them to use them as data -- & becomes %26 and + becomes %2B




please note that the above post is likely made up in its entirety.

    Viewed: 12,814 Times | Reply to This | To top
  This user is offline  sm0ke
  Subject: "re: str_replace problem" Posted: @ 7:30 pm on Nov 18 2006    

Member #: 168
Rank: User - (83)
Since: 06/05/06
Posts: 34
From: Manchester - UK

Cheers misterhaan

I'll try that out in the next rewrite and let you know how it goes.

sm0ke




Just because I dont care, doesnt mean I dont understand.

purepcs@hotmail
.co.uk |     Viewed: 12,608 Times | Reply to This | To top
Viewing Page: 1 of 1


1 |

You must be logged in to post on the forums. Login or Register








"AllSyntax.com" Copyright © 2002-2018; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank