[email protected] Posted November 20, 2009 Share Posted November 20, 2009 I have array of data(strings each containing different words) Need to check first dot(.) from right of the string. Then need to delete the remaining words starting from second(2) word example 1) this is the string.checking for period. hello1 hello2 hello3 // delete hello2 hello3 2) this is second string, want to delete. Hi Bye. Bye1 Bye2 3 4 5 6 // delete Bye2 3 4 5 6 Quote Link to comment https://forums.phpfreaks.com/topic/182230-solved-string-help/ Share on other sites More sharing options...
ldb358 Posted November 20, 2009 Share Posted November 20, 2009 heres a function that should work: function get_new_str($string){ $string = explode('.', $string); $end = strpos(trim($string['2']), ' '); $part3 = substr($string, 0, $end); return $string['0'] . $string['1'] . ' ' . $part3; } Quote Link to comment https://forums.phpfreaks.com/topic/182230-solved-string-help/#findComment-961598 Share on other sites More sharing options...
JAY6390 Posted November 20, 2009 Share Posted November 20, 2009 foreach($array_name_here as &$v) { $v = preg_replace('%\.[^\.]+$%', '', $v); } Quote Link to comment https://forums.phpfreaks.com/topic/182230-solved-string-help/#findComment-961610 Share on other sites More sharing options...
Alex Posted November 20, 2009 Share Posted November 20, 2009 I feel although I might have extremely over-complicated it.. Maybe because I just donated blood a few hours ago I'm not thinking straight.. :-\ <?php $arr = Array( 'this is the string.checking for period. hello1 hello2 hello3', 'this is second string, want to delete. Hi Bye. Bye1 Bye2 3 4 5 6' ); foreach($arr as &$val) $val = substr($val, 0, - strpos(strrev($val), '.')) . ' ' . current(explode(' ', trim(substr($val, -strpos(strrev($val), '.'))))); print_r($arr); Output: Array ( [0] => this is the string.checking for period. hello1 [1] => this is second string, want to delete. Hi Bye. Bye1 ) Quote Link to comment https://forums.phpfreaks.com/topic/182230-solved-string-help/#findComment-961613 Share on other sites More sharing options...
JAY6390 Posted November 20, 2009 Share Posted November 20, 2009 Well it works alex I didn't realise that you wanted the first word after the last period too, so here's an updated regex that will do it foreach($array_name_here as &$v) { $v = preg_replace('%\.\s*[^\s\.]+\K\s+[^\.]+$%', '', $v); } Output: Array ( [0] => this is the string.checking for period. hello1 [1] => this is second string, want to delete. Hi Bye. Bye1 ) Quote Link to comment https://forums.phpfreaks.com/topic/182230-solved-string-help/#findComment-961616 Share on other sites More sharing options...
[email protected] Posted November 20, 2009 Author Share Posted November 20, 2009 I am getting a warning, Warning: Invalid argument supplied for foreach() My code $e = array(); { ... $e[] = $stringsOfData //I am taking all the strings in $e array. ... } } foreach($e as &$v) { $v = preg_replace('%\.\s*[^\s\.]+\K\s+[^\.]+$%', '', $v); echo $v.'<br \>'; } Quote Link to comment https://forums.phpfreaks.com/topic/182230-solved-string-help/#findComment-961637 Share on other sites More sharing options...
Alex Posted November 20, 2009 Share Posted November 20, 2009 You generally get that error when the first argument supplied is not an array, and you took out the only part of the code that would be useful for identifying your issue - the part where you actually defined the contents of the array. Please post the full code. Quote Link to comment https://forums.phpfreaks.com/topic/182230-solved-string-help/#findComment-961639 Share on other sites More sharing options...
[email protected] Posted November 20, 2009 Author Share Posted November 20, 2009 if ($_POST['Submit']) { $e = array(); $para = $_POST['para']; $emails = preg_split( '@[ <>;:,\\\\/"{}*!?]@', $para ); foreach ($emails as $email) { if (empty($email)) { continue;} if(stristr($email, '@') === FALSE) { continue;} else { $e[]= $email;//.'<br \>'; continue; } } } //$regexp = '/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/'; foreach($e as &$v) { $v = preg_replace('%\.\s*[^\s\.]+\K\s+[^\.]+$%', '', $v); echo $v.'<br \>'; } Quote Link to comment https://forums.phpfreaks.com/topic/182230-solved-string-help/#findComment-961642 Share on other sites More sharing options...
Alex Posted November 20, 2009 Share Posted November 20, 2009 You have a couple of problems wrong inside of your foreach loop, and if you check the contents of $e I'm sure you'll see it's empty. Try this: if ($_POST['Submit']) { $e = array(); $para = $_POST['para']; $emails = preg_split( '@[ <>;:,\\\\/"{}*!?]@', $para ); foreach ($emails as $email) { if(empty($email) || stristr($email, '@') === FALSE) continue; $e[]= $email;//.'<br \>'; } foreach($e as &$v) { $v = preg_replace('%\.\s*[^\s\.]+\K\s+[^\.]+$%', '', $v); echo $v.'<br \>'; } } You should put the new foreach inside of the if(isset($_POST['Submit'])), so you don't get any errors if the form wasn't submitted. If you're using the continue keyword to skip an iteration there's no need for the else in the first place because if it skips the iteration it won't execute the below code anyway. Similarly you don't need to but a continue at the bottom of the foreach because it does that automatically. Finally, one other note is that you don't need to declare $e = array(); if you're using it as an array ($e[] = ..., it's optional. Quote Link to comment https://forums.phpfreaks.com/topic/182230-solved-string-help/#findComment-961644 Share on other sites More sharing options...
[email protected] Posted November 20, 2009 Author Share Posted November 20, 2009 There are some duplicate strings too... how to check and remove duplicate entries in that array? if ($_POST['Submit']) { $e = array(); $para = $_POST['para']; $emails = preg_split( '@[ <>;:,\\\\/"{}*!?=+]@', $para ); foreach ($emails as $email) { if( empty($email) || (stristr($email, '@') === FALSE) || (stristr($email, '.') === FALSE) ) continue; $e[]= $email;//.'<br \>'; } foreach($e as &$v) { $v = preg_replace('%\.\s*[^\s\.]+\K\s+[^\.]+$%', '', $v); echo $v.'<br \>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/182230-solved-string-help/#findComment-961674 Share on other sites More sharing options...
Alex Posted November 20, 2009 Share Posted November 20, 2009 array_unique Quote Link to comment https://forums.phpfreaks.com/topic/182230-solved-string-help/#findComment-961675 Share on other sites More sharing options...
[email protected] Posted November 20, 2009 Author Share Posted November 20, 2009 Thanks Alex and everyone... Quote Link to comment https://forums.phpfreaks.com/topic/182230-solved-string-help/#findComment-961690 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.