954,604 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Preg_replace problem ? space to dash

Ok. I have ZERO PHP Knowledge, still good in editing php and all that stuff. Only know copy paste and have no knowledge what that means. Still would try to explain my problem.

I use variable to fetch data from mysql. Ex. my url is http://www.example.com/venue.php?vcity=New York.

Now, I want to replace New York with New-York. The real url I am trying to get is http://www.example.com/venue-new-york.php .

For that I added code in htaccess

RewriteRule ^venue-([^/]*)\.php$ /venue.php?vcity=$1 [L]


I Know what I added in htaccess is mostly perfect.

Now, I am using smarty so would be able to do lower case and replace space with dash. But that would not allow me to fetch data from mysql for some reason. In class file I have following code.

function getallvenueschedule()
	{
		global $db,$smarty;
		$sql = " SELECT * FROM table WHERE vcity = '$this->vcity' AND sch_date >= '$this->sch_date' ORDER BY timestmp ASC";
		$theList	= $db->getAll($sql); 
		return $theList;
	}


In the venue.php file below is code.

$vcity = $_GET['vcity'];
phphit
Light Poster
28 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

Why not use the MySql REPLACE() command in your query:

SELECT *, REPLACE(vcity, ' ', '-') AS vcity_replaced FROM table WHERE vcity = '$this->vcity' AND sch_date >= '$this->sch_date' ORDER BY timestmp ASC


It might be possible to do in your htaccess, but that would require a regex that specifically targets each space, am not sure how that could be done, since you don't know how many there are going to be. You could match them, but am not sure how to replace them.

pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

Why not use the MySql REPLACE() command in your query:

SELECT *, REPLACE(vcity, ' ', '-') AS vcity_replaced FROM table WHERE vcity = '$this->vcity' AND sch_date >= '$this->sch_date' ORDER BY timestmp ASC

It might be possible to do in your htaccess, but that would require a regex that specifically targets each space, am not sure how that could be done, since you don't know how many there are going to be. You could match them, but am not sure how to replace them.

Hi,

What should replace "vcity_replaced" as ?

Moreover, will I need to change venue.php file for that purpose.

phphit
Light Poster
28 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

I don't have more than two spaces in venue (usually only one space). So if htaccess works, i would accept it as of now, if no solution comes.

phphit
Light Poster
28 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

I think this would work for one space:

RewriteRule ^venue-([^/ ]*)( ?)([^/ ]*)\.php$ /venue.php?vcity=$1-$3 [L]


I am not sure yet how to fix it for more.

pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

I am not sure yet how to fix it for more.[/QUOTE]

Ok. Any idea if same can work without htaccess. I am missing just something. (I have sent you PM also)

phphit
Light Poster
28 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

Solved in other way.

Actually no trick worked.

But what happen that url still work with "+" sign between two words (or say replace space with +).

I would not mind that "+" sign.

phphit
Light Poster
28 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: