1

I have the following string

$text =" Wireless sensor networks (WSNs) enable new applications and 
require\r\nnon-conventional paradigms for protocol design due to several 
constraints. Owing to the\r\nrequirement for low device complexity together 
with low energy consumption (i.e., long\r\nnetwork lifetime), a proper 
balance between communication and signal/data processing\r\ncapabilities 
must be found. This motivates a huge effort in research activities, 
standardization\r\nprocess, and industrial investments on this field since 
the last decade.\r\n This survey paper aims\r\nat reporting an overview of 
WSNs technologies, main applications and standards, features\r\nin WSNs 
design, and evolutions. In particular, some peculiar applications, such as 
those\r\nbased on environmental monitoring, are discussed and design 
strategies highlighted; a case\r\nstudy based on a real implementation is 
also reported. Trends and possible evolutions are\r\ntraced. Emphasis is 
given to the IEEE 802.15.4 technology, which enables many applications\r\nof 
WSNs. Some example of performance characteristics of 802.15.4-based networks 
are\r\nshown and discussed as a function of the size of the WSN and the data 
type to be exchanged\r\namong nodes. ";

I want to replace \r\n with nothing only if it was proceeded by dot (.); remove all \r\n but keep .\r\n I wrote the following function:

function ReplaceText($haystack,$needle){
$lastPos = 0;
$positions = array();
$rText = "";
while (($lastPos = strpos($haystack, $needle, $lastPos))!== false)
{
    $positions[] = $lastPos;
    if (substr($haystack, $lastPos-1, 1) == '.') {
      // do nothing
    } else {
        $rText .= substr($text, 0, $lastPos).str_replace('\r\n', '', substr($text, $lastPos));
    }
    $lastPos = $lastPos + strlen($needle);
}
return $rText;
}

It's not working!

9
  • 1
    And by "It's not working" you mean?
    – Spoody
    Commented Mar 10, 2018 at 18:29
  • 1
    str_replace(".\r\n", '', $text)
    – u_mulder
    Commented Mar 10, 2018 at 18:29
  • @u_mulder This would remove even the preceding dot. Commented Mar 10, 2018 at 18:36
  • 1
    Your initial question and your last comment are like 100% contradictory.
    – ishegg
    Commented Mar 10, 2018 at 18:43
  • 1
    This means "remove \r\n if it is not preceded by ."
    – u_mulder
    Commented Mar 10, 2018 at 18:46

3 Answers 3

3

As str_replace replaces symbols from left to right, you can use this tricky solution:

echo str_replace([".\r\n", "\r\n", '__xyz__'],['__xyz__', '', ".\r\n"],$text);
// first replace `.\r\n` with some unique list sequence `__xyz__`
// then replace the remaining `\r\n`
// then replace unique list sequence `__xyz__` back with `.\r\n`

Regexp solution is here:

echo preg_replace("/([^\.])\r\n/", '$1', $text);
5
  • i think this is a quick solution. but if there is a words __xyz__ in the paragraph then it will produce unintended result. but i think __xyz__ should be fine Commented Mar 10, 2018 at 18:53
  • 1
    @AZakaria well.. you can vote this as accepted answer if this answer help you to solve your problem Commented Mar 10, 2018 at 19:08
  • Unbelievably clever. Wow.
    – suchislife
    Commented Mar 10, 2018 at 19:17
  • I'm trying to vote, but it says I cannot since my reputation is less than 15! how can increase my reputation?!
    – A Zakaria
    Commented Mar 10, 2018 at 19:17
  • 1
    @AZakaria But you can accept this answer :) Commented Mar 10, 2018 at 19:35
1

You could use negative lookbehind in regular expression:

$text = preg_replace('~(?<!\.)\r\n~', ' ', $text);

Example :

$text = "foo\r\nbar.\r\ntest";
$text = preg_replace('~(?<!\.)\r\n~', ' ', $text);
echo $text; // "foo bar.\r\ntest"
0

Here's a working example for you to learn from...

<?php 

// The haystack
$haystack1 =" Wireless sensor networks (WSNs) enable new applications and 
require\r\nnon-conventional paradigms for protocol design due to several 
constraints. Owing to the\r\nrequirement for low device complexity together 
with low energy consumption (i.e., long\r\nnetwork lifetime), a proper 
balance between communication and signal/data processing\r\ncapabilities 
must be found. This motivates a huge effort in research activities, 
standardization\r\nprocess, and industrial investments on this field since 
the last decade.\r\n This survey paper aims\r\nat reporting an overview of 
WSNs technologies, main applications and standards, features\r\nin WSNs 
design, and evolutions. In particular, some peculiar applications, such as 
those\r\nbased on environmental monitoring, are discussed and design 
strategies highlighted; a case\r\nstudy based on a real implementation is 
also reported. Trends and possible evolutions are\r\ntraced. Emphasis is 
given to the IEEE 802.15.4 technology, which enables many applications\r\nof 
WSNs. Some example of performance characteristics of 802.15.4-based networks 
are\r\nshown and discussed as a function of the size of the WSN and the data 
type to be exchanged\r\namong nodes. ";

// The first needle
$needle1 = ".\r\n";
// The second needle
$needle2 = "\r\n";


// First we find all occurrances of .\r\n and replace it with a ramdom_text_of_your_choice.
$results1 = ReplaceText1($haystack1,$needle1);



// We are now free to remove only the \r\n instances...
$results2 = ReplaceText2($haystack1,$needle2);



// Finally we change ramdom_text_of_your_choice back to .\r\n
$results3 = ReplaceText3($haystack1,$needle1);

echo $results3;



// The function
function ReplaceText1($haystack1,$needle1){

return str_replace($needle1, 'ramdom_text_of_your_choice', $haystack1);

}

// The function
function ReplaceText2($haystack1,$needle2){

return str_replace($needle2, '', $haystack1);

}

// The function
function ReplaceText3($haystack1,$needle1){

return str_replace($needle1, '.\r\n', $haystack1);

}


?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.