Jump to content

[SOLVED] Strip matching string 'segments'


shatner

Recommended Posts

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

 

Link to comment
Share on other sites

<?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>';
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.