Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem with stristr text matching in PHP
    primarykey
    data
    text
    <p>I'm running a fairly simple script which tries to match strings from a csv file with potential matches in a mysql table (collation: ut8_general_ci). For each row in the csv file, I pull out the string (haystack) I want, which looks something like this:</p> <p><strong>"Full Cmte. Member City of Rutland Rutland VT"</strong></p> <p>For each string, I pull the list of matches from my db, and cycle through them until stristr finds a match. (I'm using stristr instead of regex because it's simpler and (I think?) quicker.) Some of the matching strings don't make grammatic/syntactical sense because they're constructed as aliases particular to this data set. One of them is "City of Rutland Rutland VT" (an alias for "City of Rutland (VT)"), which should, but doesn't, match the string above. For more than 90% of these matches, I don't have any problems. However, certain text matching doesn't seem to work. </p> <p>Here are a list of those that fail to produce a match:</p> <p>Haystack => Needle</p> <ul> <li>"Full Cmte. Member City of Ocala Ocala FL" => "City of Ocala Ocala FL"</li> <li>"Full Board Member Water and Sanitation District Anthony NM" => "Water and Sanitation District Anthony"</li> <li>"Energy Clean Air & Climate Change Subcmte Member Consol Inc." => "Consol Inc."</li> <li>"Full Council Member; Sr. VP Integrated Services Burke Inc. Cincinnati OH" => "Burke Inc."</li> <li>"City of San Antonio TX" => "City of San Antonio TX"</li> <li>"Full Cmte member United National Indian Tribal Youth Inc. (UNITY)" => "United National Indian Tribal Youth Inc."</li> <li>"ECA&CC Sub. Member Cyprus Amax Minerals Inc." => "Cyprus Amax Minerals Inc."</li> <li>"Silcon Valley Manufacturing Group" => "Silcon Valley Manufacturing Group"</li> <li>"President Global Environmental Resources Inc. Washington DC" => "Global Environmental Resources Inc."</li> <li>"Lancaster Laboratories Inc." => "Lancaster Laboratories Inc."</li> </ul> <p>I'm not sure what to make of this, unless it's something very basic that I've totally missed. it seems that most of the errors have "inc." in the match, but not sure that's what's causing it.</p> <p>Here's the code (though the answer below fit the bill):</p> <pre><code>$patterns = array(); $patterns[0] = '/\s+/'; $patterns[1] = '/&amp;/'; $replacement = array(); $replacement[0] = ' '; $replacement[1] = 'and'; $name = trim(preg_replace($patterns,$replacement,$name)); if(stristr($name,trim(preg_replace($patterns,$replacement,$org-&gt;org_name)))) { // code here } </code></pre> <p>It's not terribly graceful right now and I would appreciate any additional insight as to how to normalize strings for matching.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload