Jump to content


Photo

help with preg_replace ?


  • Please log in to reply
8 replies to this topic

#1 slashpine

slashpine
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 18 September 2006 - 10:07 PM

I am guessing this "preg_replace" is the best option in this?

using the code below ( see post)
http://www.phpfreaks...c,106136.0.html
many of the addresses (addr1) have a # followed by either a letter or a number i.e., # A , # 400  etc. etc.
Can I use preg_replace?  so the scritp ignores the # and everything following it in that column ?

    $url = "http://us.rd.yahoo.com/maps/us/insert/Tmap/extmap/*-http://maps.yahoo.com/maps_result?addr=";
    $url.= str_replace(" ","+",$query_data['addr1']);
    $url.= "&csz=";
    $url1 = str_replace(",","%2C",$query_data['addr2']);
    $url.= str_replace(" ","+",$url1). "&country=us";
    echo "<a href='". $url. "'><img src='images/maplink.gif' width='89' height='17' alt=''/></a>";


#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 18 September 2006 - 10:14 PM

preg_replace('/#.*$/', '', $variable);
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 slashpine

slashpine
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 18 September 2006 - 10:38 PM

sorry for the wrong placement...

the script ignores the line... no errors but the # and what follows prevent the script from working

?

#4 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 18 September 2006 - 10:46 PM

What does your code look like?
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#5 slashpine

slashpine
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 18 September 2006 - 10:52 PM

$url = "http://us.rd.yahoo.com/maps/us/insert/Tmap/extmap/*-http://maps.yahoo.com/maps_result?addr=";
    $url = "http://us.rd.yahoo.com/maps/us/insert/Tmap/extmap/*-http://maps.yahoo.com/maps_result?addr=";
    $url.= str_replace(" ","+",$row['address']);
    $url.= "&amp;csz=";
    $url1 = str_replace(",","%2C",$row['city']);
    $url.= str_replace(" ","+",$url1). ",GA+&amp;country=us";

this is edited from the  code in the original in thread I cited for my column names/query

This works great as long as there is no # A or # 800  etc after the street address ($address)

#6 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 18 September 2006 - 10:54 PM

I don't see the preg_replace in your code--how are you using it?
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#7 slashpine

slashpine
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 18 September 2006 - 10:58 PM

$url.= preg_replace('/#.*$/', '', $row['address']);

#8 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 19 September 2006 - 12:34 AM

Works for me:

<pre>
<?php

	$row = array(
		address => '123 Main St. #4'
	);
	echo $url.= preg_replace('/#.*$/', '', $row['address']);

?>
</pre>

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#9 slashpine

slashpine
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 19 September 2006 - 12:40 AM

thanks for the reply but I don't see how to apply that to the code cited...the script parses the "address" from the database and builds a URL that displays  a map from Yahoo

can you expand on how I can emply this or possibly suggest another way of  eliminating the problem of the # and what follows...?

Thanks again...

[edited]

I solved this problem by changing the "#" character in the table column to the word "Unit" this allows the the script tpo parse the correct address into the URL (script)

Thanks again for all the help...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users