简体   繁体   English

在 PHP 数组中查找和替换多行短语/文本而不损坏数组

[英]Find and Replace Multi-Row Phrases/text Within PHP Arrays without damaging the array

Lets say I have an array:假设我有一个数组:

$myarray = (
 [0] => 'Johnny likes to go to school',
 [1] => 'but he only likes to go on Saturday',
 [2] => 'because Saturdays are the best days', 
 [3] => 'unless you of course include Sundays',
 [4] => 'which are also pretty good days too.',
 [5] => 'Sometimes Johnny likes picking Strawberrys',
 [6] => 'with his mother in the fields',
 [7] => 'but sometimes he likes picking blueberries'
);

Keeping the structure of this array intact, I want to be able to replace phrases within it, even if they spill over to the next or previous string.保持这个数组的结构完整,我希望能够替换其中的短语,即使它们溢出到下一个或上一个字符串。 Also I don't want punctuation or case to impact it.我也不希望标点符号或大小写影响它。

Examples:例子:

String to Find: "Sundays which are also pretty good"要查找的字符串: “周日也不错”

Replace with: "Mondays which are also pretty great"替换为: “周一也很棒”

After replace:更换后:

$myarray = (
     [0] => 'Johnny likes to go to school',
     [1] => 'but he only likes to go on Saturday',
     [2] => 'because Saturdays are the best days', 
     [3] => 'unless you of course include Mondays', 
     [4] => 'which are also pretty great days too.',
     [5] => 'Sometimes Johnny likes picking Strawberrys',
     [6] => 'with his mother in the fields',
     [7] => 'but sometimes he likes picking blueberries'
    );

Curious if there is an ideal way of doing this.好奇是否有一种理想的方法来做到这一点。 My original thought was, I would turn the array into a string, strip out punctuation and spaces, count the characters and replace the phrase based on the character count.我最初的想法是,将数组变成字符串,去掉标点符号和空格,计算字符数并根据字符数替换短语。 But, it is getting rather complex, but it is a complex problem但是,它变得相当复杂,但这是一个复杂的问题

This is a job for regular expressions.这是正则表达式的工作。

The method I used was to implode the array with some glue (ie '&' ).我使用的方法是用一些glue (即'&' )内implode数组。 Then I generated a regular expression by inserting a zero-or-one check for '&' in between each character in the find string.然后我通过在查找字符串中的每个字符之间插入一个零或一个检查'&'来生成一个正则表达式。

I used the regular expression to replace occurrences of the string we were looking for with the replacement string.我使用正则表达式用替换字符串替换了我们正在寻找的字符串的出现。 Then I explode d the string back into an array using the same delimiter as above ( '&' )然后我explode D使用相同的定界符与上述字符串返回到一个数组( '&'

$myarray = [
    0 => 'Johnny likes to go to school',
    1 => 'but he only likes to go on Saturday',
    2 => 'because Saturdays are the best days',
    3 => 'unless you of course include Mondays',
    4 => 'which are also pretty great days too.',
    5 => 'Sometimes Johnny likes picking Strawberrys',
    6 => 'with his mother in the fields',
    7 => 'but sometimes he likes picking blueberries'
];

// preg_quote this because we're looking for the string, not for a pattern it might contain
$findstring = preg_quote("Sundays which are also pretty good");
// htmlentities this in case it contains a string of characters which happen to be an html entity
$replacestring = htmlentities("Mondays which are also pretty great");

// Combine array into one string
// We use htmlentitles to escape the ampersand, so we can use it as a sentence delimeter
$mystring = implode("&", array_map('htmlentities', $myarray));

// Turns $findString into:
// S\&?u\&?n\&?d\&?a\&?y\&?s\&? \&?w\&?h\&?i\&?c\&?h\&? \&?a\&?r\&?e\&?
// \&?a\&?l\&?s\&?o\&? \&?p\&?r\&?e\&?t\&?t\&?y\&? \&?g\&?o\&?o\&?d
$regexstring = implode("\&?", str_split($findstring));

// Johnny likes to go to school&but he only likes to go on Saturday&because Saturdays are the
// best days&unless you of course include Mondays&which are also pretty great days
// too.&Sometimes Johnny likes picking Strawberrys&with his mother in the fields&but sometimes
// he likes picking blueberries
$finalstring = preg_replace("/$regexstring/", $replacestring, $mystring);

// Break string back up into array, and return any html entities which might have existed
// at the beginning.
$replacedarray = array_map('html_entity_decode', explode("&", $finalstring));

var_dump($replacedarray);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM