PHP替换字符串中的某些事件

时间:2018-03-10 18:28:33

标签: php str-replace substr

我有以下字符串

$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. ";

我想用\ n \ n来替换\ r \ n,只有它是由点(。)进行的;删除所有\ r \ n但保留。\ r \ n 我写了以下函数:

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

它没有用!

3 个答案:

答案 0 :(得分:3)

str_replace从左到右替换符号时,您可以使用这个棘手的解决方案:

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解决方案就在这里:

echo preg_replace("/([^\.])\r\n/", '$1', $text);

答案 1 :(得分:1)

您可以在正则表达式中使用负向lookbehind:

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

示例:

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

答案 2 :(得分:0)

以下是一个可以向您学习的工作示例......

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

}


?>