shatner Posted December 2, 2008 Share Posted December 2, 2008 Hi Guys, I’m hoping you can help; I have a list of items which all contain a similar string e.g.: This is line number 1 This is colour green This is a silly example So all the above contain ‘This is’; I want to create a dropdown menu with this bit removed (But I won’t know what the ‘same’ bit will be) The dropdown in the example above would consist of the following: <select name="select"> <option value="1">line number 1 </option> <option value="2">colour green </option> <option value="3">a silly example </option> </select> Any ideas on a good way to go about this? , is there a function to remove matching string sections from two initial strings/array items? Any help would be very much appreciated Shatner Quote Link to comment https://forums.phpfreaks.com/topic/135136-solved-strip-matching-string-segments/ Share on other sites More sharing options...
ranjuvs Posted December 2, 2008 Share Posted December 2, 2008 <?php $strings = array("This is line number 1", "This is colour green", "This is a silly example"); $replace = "This is"; $new_strings = array(); $strings = str_replace($replace,'',$strings); echo "<select name='string'>"; foreach($strings as $string) { echo "<option value=''>$string</option>"; } echo '</select>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/135136-solved-strip-matching-string-segments/#findComment-703843 Share on other sites More sharing options...
shatner Posted December 2, 2008 Author Share Posted December 2, 2008 Thanks for that but I'm not going to know which bit to strip out, it wont be the same 'This is' string every time so that wont work. Think i've got it working by splitting the string into an array of words and then using in_array to look for duplicates. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/135136-solved-strip-matching-string-segments/#findComment-703849 Share on other sites More sharing options...
Daniel0 Posted December 2, 2008 Share Posted December 2, 2008 Hmm... interesting. This will do it if I understand you correctly: <?php $strings = $cp = array( 'This is line number 1', 'This is colour green', 'This is a silly example', ); uasort($cp, create_function('$a,$b', 'return strlen($a) > strlen($b) ? 1 : -1;')); $max = strlen(array_shift($cp)); unset($cp); $common = null; $pos = 0; for ($i = 0; $i < $max; $i++) { foreach ($strings as $s) { if (!isset($c)) { $c = substr($s, $i, 1); } else { if ($c !== substr($s, $i, 1)) { break 2; } } } $common .= $c; unset($c); $pos = $i; } if (strlen($common) > 0) { echo '<label for="foo">' . trim($common) . '…</label>' . PHP_EOL . '<select name="foo" id="foo">' . PHP_EOL; foreach ($strings as $index => $string) { echo "\t" . '<option option="' . $index . '">… ' . trim(substr($string, $pos)) . '</option>' . PHP_EOL; } echo '</select>'; } else { echo 'The strings do not share any common starting data...'; } ?> Output: <label for="foo">This is…</label> <select name="foo" id="foo"> <option option="0">… line number 1</option> <option option="1">… colour green</option> <option option="2">… a silly example</option> </select> Quote Link to comment https://forums.phpfreaks.com/topic/135136-solved-strip-matching-string-segments/#findComment-703860 Share on other sites More sharing options...
shatner Posted December 2, 2008 Author Share Posted December 2, 2008 That’s fantastic, exactly what I needed! Thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/135136-solved-strip-matching-string-segments/#findComment-703874 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.