Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

PHP QUESTION: How to use str_replace?


JEWbacca

Recommended Posts

Posted

Good Afternoon,

 

I'm trying to strip out speical characters such as ?, ?, etc from product names when a search is beind done. Reason being is that if a customer does a search for say example 'SONY' a number of products come up (around 60 or so...). The problem is that each 'SONY' product has a ? symbol after the word 'SONY' (ie-SONY?). This keeps customers from inputing more text to narrow down their search.

 

 

Contributions in use: AJAX QUICK SEARCH http://www.oscommerce.com/community/contributions,3413

 

An example of this contribution in use is listed on the contribution page, but here it is again -- http://smartprodukter.com/advanced_search.php

 

 

After spending some time at php.net I think that using a function such as str_replace (http://www.php.net/str_replace)is what I need to do, but i'm at a bit of a loss.

 

Anyone have any experince with this? My goal is to change ? and ? to just a blank space.

 

Thanks in advance,

 

Nate

Posted
Good Afternoon,

 

I'm trying to strip out speical characters such as ?, ?, etc from product names when a search is beind done. Reason being is that if a customer does a search for say example 'SONY' a number of products come up (around 60 or so...). The problem is that each 'SONY' product has a ? symbol after the word 'SONY' (ie-SONY?). This keeps customers from inputing more text to narrow down their search.

Contributions in use: AJAX QUICK SEARCH http://www.oscommerce.com/community/contributions,3413

 

An example of this contribution in use is listed on the contribution page, but here it is again -- http://smartprodukter.com/advanced_search.php

After spending some time at php.net I think that using a function such as str_replace (http://www.php.net/str_replace)is what I need to do, but i'm at a bit of a loss.

 

Anyone have any experince with this? My goal is to change ? and ? to just a blank space.

 

Thanks in advance,

 

Nate

Using str_replace() probably won't do yuo any good. The reason is that most of the time, symbols are ASCII characters. For instance, to create the copyright symbol in HTML use "©" OR "©".

 

I just did a quick Google search for "ascii html" (without the quotes) and found http://ascii.cl/htmlcodes.htm

--

I need no warrant for being, and no word of sanction upon my being. I am the warrant and the sanction.

Ayn Rand

Posted

Raquel,

 

I looked in database, it seems that these characters (?, ?, ?, etc...) are infact there in plain form.

 

Do you still think I will have an issue w/ using the str_replace() function or somthing like it? If so do you have any other suggestions? If not, could you possibley give me an example of how I would use the str_replace() to convert such characters to exmty space?

 

Thanks,

 

Nate

Posted
Raquel,

 

I looked in database, it seems that these characters (?, ?, ?, etc...) are infact there in plain form.

 

Do you still think I will have an issue w/ using the str_replace() function or somthing like it? If so do you have any other suggestions? If not, could you possibley give me an example of how I would use the str_replace() to convert such characters to exmty space?

 

Thanks,

 

Nate

 

$myvariable = str_replace(?, '', $myvariable)

Posted

Robert,

 

Thanks for the reply.

 

I'm confused by the use of $myvariable? Do I need to define what $myvariable is?

Also, if i'm right... that will replace ? with whatever $myvariable is set to... can i just set it to nothing (a black space)?

 

Thanks,

 

Nate

 

$myvariable = str_replace(?, '', $myvariable)
Posted
Robert,

 

Thanks for the reply.

 

I'm confused by the use of $myvariable? Do I need to define what $myvariable is?

Also, if i'm right... that will replace ? with whatever $myvariable is set to... can i just set it to nothing (a black space)?

 

Thanks,

 

Nate

 

I'm trying to strip out speical characters such as ?, ?, etc from product names when a search is beind done

The variable that holds "product names".

 

I don't have time to look atm, I've not visited that file.

Posted

And no it would replace with nothing

 

$myvariable = str_replace(?, '', $myvariable)

 

Is effectively doing the following: -

 

$variable = replace ? with nothing in $variable

Posted

And my initial code was wrong ? should have been '?'

 

$myvariable = str_replace('?', '', $myvariable);

Posted

Robert,

 

It seems to work good at taking out the ? with this function:

 

		$name = $row['products_name'];
$fixed = str_replace(?, '', $name);

 

The problem is... it takes it out from displaying, but my guess is not from being what is searched

 

 

Here is a link to my development stores advanced search page -- http://ipodcarparts.com/teststore/advanced_search.php

 

If you type in Prolink you will see a number of products show up.. but if you continue typing one of the product titles it will not work because there is a ? (that is now hidden from being displayed) after the word ProLink.

 

I'm loss for ideas.. any clue?

Posted
Robert,

 

It seems to work good at taking out the ? with this function:

 

		$name = $row['products_name'];
$fixed = str_replace(?, '', $name);

 

The problem is... it takes it out from displaying, but my guess is not from being what is searched

Here is a link to my development stores advanced search page -- http://ipodcarparts.com/teststore/advanced_search.php

 

If you type in Prolink you will see a number of products show up.. but if you continue typing one of the product titles it will not work because there is a ? (that is now hidden from being displayed) after the word ProLink.

 

I'm loss for ideas.. any clue?

 

You need to take it out before the search is done. e.g. from the input form

 

Going to work now talk later.

Posted

I took another look at this, but still i'm not sure. While I can now keep the characters from displaying... they still are characters which I guess the script expects the user to include in their search-- and when they don't it causes a problem.

 

I guess it needs to be removed before the query is returned to the script from the database? Does that sound right?

 

You need to take it out before the search is done. e.g. from the input form

 

Going to work now talk later.

Posted
I took another look at this, but still i'm not sure. While I can now keep the characters from displaying... they still are characters which I guess the script expects the user to include in their search-- and when they don't it causes a problem.

 

I guess it needs to be removed before the query is returned to the script from the database? Does that sound right?

 

The query could use "WHERE LIKE %$thesearch%"

 

The idea being that if the search is "FOO" it would find ?FOO and ?FOO?

Posted

Hmm.. but the issue is that as soon as a chacarter which is expected does now show up... the search stops narrowing down the results.

 

Here is the code used in quickfind.php which AJAX uses to create/populate the grey little box.

 

<?php
/*
 $Id: quickfind.php,v 1.10 2005/08/04 23:25:46 hpdl Exp $

 osCommerce, Open Source E-Commerce Solutions
 http://www.oscommerce.com

 Copyright ? 2003 osCommerce

 Released under the GNU General Public License

*/
// output a response header
header('Content-type: text/html; charset=ISO-8859-1');

require('includes/application_top.php');
$results = array();
$q = '';
$name = '';
$fixed = '';
$id = '';
$url = '';
$osCsid = '';

$q = addslashes(preg_replace("%[^0-9a-zA-Z ]-%", "", $_GET['keywords']) );
$osCsid = addslashes(preg_replace("%[^0-9a-zA-Z ]%", "", $_GET[tep_session_name()]) );

$limit = 55;

 if ( isset($q) && tep_not_null($q) ) {
  $query = tep_db_query("select pd.products_id, pd.products_name, p.products_model from " . TABLE_PRODUCTS_DESCRIPTION . " pd left join " . TABLE_PRODUCTS . " p on (p.products_id = pd.products_id) where (pd.products_name like '%" . tep_db_input($q) . "%' or p.products_model like '%" . tep_db_input($q) . "%') and p.products_status = '1' and pd.language_id = '" . (int)$languages_id . "' order by pd.products_name asc limit " . $limit);

if ( tep_db_num_rows($query) ) {
  while ( $row = tep_db_fetch_array($query) ) {

	if ( isset($row['products_model']) && tep_not_null($row['products_model']) ) {
	  $model = ' [' . $row['products_model'] . ']';
	} else {
	  $model = '';
	}

	$name = $row['products_name'];
$fixed = str_replace(?, '', $name);
	$id = $row['products_id'];
	$url = tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $id);
	$results[] = '<a href="' .  $url . '">' .  $fixed . '</a>' . $model . "\n";
	// $results[] = '<a href="' . $url . '">' . $name . $model . '</a>' .  "\n";
  }
} else {
  $results [] = 'No Quick Find Results';
}
echo implode('<br>' . "\n", $results);
// To use <DOCTYPE> XHTML 1.0 or higher
// echo implode('<br />' . "\n", $results); 
 } else {
echo "Quick Find Results ...";
 }

?>

 

As for the advanced_search.php the changes made inlcude adding

 

var req; 

function loadXMLDoc(key) {

  var url="quickfind.php?osCsid=<?php echo tep_session_id();?>&keywords="+key;

  // Internet Explorer
  try { req = new ActiveXObject("Msxml2.XMLHTTP"); } 
  catch(e) { 
  try { req = new ActiveXObject("Microsoft.XMLHTTP"); } 
  catch(oc) { req = null; } 
  } 

  // Mozailla/Safari 
  if (!req && typeof XMLHttpRequest != "undefined") { req = new XMLHttpRequest(); } 

  // Call the processChange() function when the page has loaded 
  if (req != null) {
  req.onreadystatechange = processChange; 
  req.open("GET", url, true); 
  req.send(null); 
  } 
} 

function processChange() { 
  // The page has loaded and the HTTP status code is 200 OK 
  if (req.readyState == 4 && req.status == 200) { 

  // Write the contents of this URL to the searchResult layer 
  getObject("quicksearch").innerHTML = req.responseText;
  } 
} 

function getObject(name) { 
  var ns4 = (document.layers) ? true : false; 
  var w3c = (document.getElementById) ? true : false; 
  var ie4 = (document.all) ? true : false; 

  if (ns4) return eval('document.' + name); 
  if (w3c) return document.getElementById(name); 
  if (ie4) return eval('document.all.' + name); 
  return false; 
}


window.onload = function() { 
  getObject("keywords").focus();
}

 

Right after

  <script language="javascript"><!--

 

and replacing this:

 

  <?php
$info_box_contents = array();
$info_box_contents[] = array('text' => HEADING_SEARCH_CRITERIA);

new infoBoxHeading($info_box_contents, true, true);

$info_box_contents = array();
$info_box_contents[] = array('text' => tep_draw_input_field('keywords', '', 'style="width: 100%"'));
$info_box_contents[] = array('align' => 'right', 'text' => tep_draw_checkbox_field('search_in_description', '1') . ' ' . TEXT_SEARCH_IN_DESCRIPTION);

new infoBox($info_box_contents);
 ?>

 

with this:

 

<?php
 $info_box_contents = array();
 $info_box_contents[] = array('text' => HEADING_SEARCH_CRITERIA);

 new infoBoxHeading($info_box_contents, true, true);

 $info_box_contents = array();
 $info_box_contents[] = array('text' => tep_draw_input_field('keywords', '', 'id="keywords" onKeyUp="loadXMLDoc(this.value)" autocomplete="off" style="width: 100%"'));
 $info_box_contents[] = array('text' => '<div style="display: block; margin-left: 0%; width:100%; float: left;border:solid 1px;background-color:#CCCCCC;" id="quicksearch">Quick Find Results....</div>');
 $info_box_contents[] = array('align' => 'right', 'text' => tep_draw_checkbox_field('search_in_description', '1') . ' ' . TEXT_SEARCH_IN_DESCRIPTION);

 new infoBox($info_box_contents);
?>

 

I don't see where it is called and where I could put a similar replace function in after the customer puts in what they want, but before it is searched in databse..

 

Any idea or another method to do somthing that would work...?

 

Thanks,

 

Nate

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...