Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP String Differences and Dynamic Restrictions
    primarykey
    data
    text
    <p><strong>Example A [the simplified one]: --------------------------------------------------------------------</strong></p> <blockquote> <p><strong>MODEL</strong>: String <strong><em>{1}</em></strong> and keeeeeep on <strong><em>{2}</em></strong> and oooon...</p> <p><strong>CASE_A</strong>: String <strong><em>hello</em></strong> and keeeeeep on <strong><em>two words</em></strong> and oooon...</p> <p><strong>CASE_B</strong>: String <strong><em>anything else</em></strong> and keeeeeep on <strong><em>just for fun</em></strong> and oooon...</p> </blockquote> <p>I need to get a list with <em>n</em> vars called <code>$v1</code>, <code>$v2</code>, <code>$vn</code>... with their respective matched values:<br><br> <strong>EDIT</strong>:<br>Note that the variable names are given depending on the placeholder. Placeholders are always an INT. <em>(The numbers are just indexes not word count)</em><br><br> For <strong>case A</strong>:<br> <code>$v1</code>=<em>hello</em><br> <code>$v2</code>=<em>two words</em><br> <code>$vn</code>=<em>etc</em>...<br><br> For <strong>case B</strong>:<br> <code>$v1</code>=<em>anything else</em> <br> <code>$v2</code>=<em>just for fun</em><br> <code>$vn</code>=<em>etc</em>...<br><br> As you can see the references to get these values are the "<em>constant</em>" parts of both strings.</p> <p><strong>Example B [the almost-real one]: --------------------------------------------------------------------</strong></p> <p>Now we suppose that each <em>possible match</em> is saved in an array <em>(real case is a long database)</em>, like this:<br><br> <strong>possible_matches</strong>{<br> [0] <em>string three</em> &lt;&lt;&lt;&lt;<br> [1] <em>Oneword</em> &lt;&lt;&lt;&lt;<br> [2] <em>Other stuff</em><br> [3] <em>Harry Poppotter</em><br> [4] <em>two words</em> &lt;&lt;&lt;&lt;<br> [5] <em>Magic words magic feelings</em><br> }</p> <p>In the previous example it was not necessary, because each <strong>{n}</strong> "<em>placeholder</em>" was separated by "<em>constant</em>" strings. But there are cases where these "<em>placeholders</em>" are together... so I have to invent a new way to match the <strong>possible matches</strong> (fixed list). </p> <blockquote> <p>String <strong><em>{1}</em></strong> and keeeeeep on <strong><em>{2}</em></strong> <em><strong>{3}</em></strong> and oooon...</p> <p>String <strong><em>Oneword</em></strong> and keeeeeep on <strong><em>two words</em></strong> <em><strong>string three</em></strong> and oooon...</p> </blockquote> <p>As you can see <em>(based on the array shown above)</em>, the result should be:<br> <code>$v1</code>=<em>hello</em><br> <code>$v2</code>=<em>two words</em><br> <code>$v3</code>=<em>string three</em><br><br> But how knows PHP how I want my strings <strong><em>to be separated</em></strong>?<br><br> <strong>My idea</strong> is doing the next:<br><br> 1) Get the {2}{3} block as a <strong>single one</strong>.<br> 2) Check in the array if this block <em>(two words and three words)</em> is <code>in_array()</code><br> 3) If NOT:<br> 4) Remove the last word of it<br> 5) Check again with the new <em>(two words and three)</em>.<br> 6) If NOT:<br> &lt;&lt;&lt;&lt;<br> 4') Remove the last word of it<br> 5') Check again with the new <em>(two words and)</em>.<br> 4'') Remove the last word of it<br> 5'') Check again with the new <em>(two words)</em>.<br> &lt;&lt;&lt;&lt;<br> 7) Repeat 4 and 5 till it is one <strong>possible match</strong> (<code>in_array()</code>)<br> 8) The matched one will be <strong>{2}</strong> and the rest of the string will be <strong>{3}</strong><br><br> <strong>My question</strong>: how can I make this, in PHP?<br> I tried to explain it the most simplified I could, and I hope you understand what I am trying to ask for. If anyone needs more examples I will write them down, just let me know. Thanks for reading.<br><br> <strong>EDIT --------------------------------------------------------------------</strong><br>A real example:<br><br> Array: <strong>possible_matches</strong>{<br> [0] <em>Christopher Johnson</em><br> [1] <em>McCandless</em><br> [2] <em>cinema</em><br> [3] <em>tomorrow at night</em><br> }<br></p> <blockquote> <p><strong>MODEL</strong>: My name is <strong><em>{1}</em></strong> <em><strong>{2}</em></strong>, and I am going to the <strong><em>{3}{4}</em></strong></p> <p><strong>CASE</strong>: My name is <strong><em>Christopher Johnson</em></strong> <em><strong>McCandless</em></strong>, and I am going to the <strong><em>cinema tomorrow at night</em></strong></p> </blockquote> <p><strong>Desired</strong> result:<br> <code>$v1</code>=<em>Christopher Johnson</em><br> <code>$v2</code>=<em>McCandless</em><br> <code>$v3</code>=<em>cinema</em><br> <code>$v4</code>=<em>tomorrow at night</em><br><br> <strong>Create possible combinations array</strong><br></p> <pre><code>function get_possible_groups($string_of_words, $groups_count){ $words=explode(' ',$string_of_words); $total=count($words); $group_1=array(array()); $group_2=array(array()); //We can create TOTAL-1 combinations for($i=0;$i&lt;$total;$i++){ $lim=$total-$i-1; for($j=0;$j&lt;$total;$j++){ if($j&lt;$lim){ $group_1[$i][]=$words[$j]; }else{ $group_2[$i][]=$words[$j]; } } } return array($group_1,$group_2); } </code></pre> <p><strong>UPDATE referenced in comments for the <em>ACCEPTED</em> answer</strong><br></p> <pre><code>$model="Damn you {1}, {2} will kill you. {3}{4}{5}"; //Array =&gt; Save how many single placeholders are in each "placeholder block" $placeholder_count{ [0]=1, //first block contains one placeholder [1]=1, //second block contains one placeholder [2]=3 //third block contains three placeholders } //Simplify all blocks into ONE SINGLE regex placeholder $pattern="/Damn you (.*), (.*) will kill you. (.*)/"; //Match in string $string="Damn you Spar, Will will kill you. I Love it man."; preg_match($pattern,$string,$matches); //View in array which placeholders have to be checked $block_0=$matches[1]; //Array shows it was 1 p.holder. No check needed $block_1=$matches[2]; //Array shows it was 1 p.holder. No check needed $block_2=$matches[3]; //It shows it were 3 p.holders. Possible grouping (n=3) //Result $v1=$matches[1]; $v2=$matches[2]; $v3,$v4,$v5=(Result of grouping and querying the $matches[3] with groups_count=3) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    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