Jump to content

[SOLVED] String help


salman_ahad@yahoo.com

Recommended Posts

 

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

 

Link to comment
Share on other sites

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
)

Link to comment
Share on other sites

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
)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 

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 \>';
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 

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 \>';
}
}

 

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.