не получается заменить текст с помощью php
У нас все тайтлы товаров в таком виде Игровой ноутбук Lenovo IdeaPad L340-15IRH 15.6"[(1920×1080)/Intel Core i5 9300H (2.4-4.1 ГГц)/8192/1000(5400)/128/NVidia GTX 1050(3 072)/DOS/нет/BT]
Хочу заменить скобки на div style="display:none" чтобы небыло видно характеристик, но не получается совсем, пытался через jquery но почему то он меняет весь текст. Пробую через php, на выходе пусто
<?if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)die();
if(empty($arResult))
return "";
$strReturn = "";
$itemSize = count($arResult);
for($index = 0; $index < $itemSize; $index++) {
$title = htmlspecialcharsex($arResult[$index]["TITLE"]);
$patterns = array();
$patterns[0] = "[";
$patterns[1] = "]";
$replacements = array();
$replacements[0] = "<div style='display:none'>";
$replacements[1] = "</div>";
$newtitle = preg_replace($patterns, $replacements, $title);
$nextRef = ($index < $itemSize-2 && $arResult[$index+1]["LINK"] <> "" ? " itemref='breadcrumb_".($index + 1)."'" : "");
$child = ($index > 0 ? " itemprop='child'" : "");
$arrow = ($index > 0 ? "<span class='breadcrumb__arrow'></span>" : "");
if($arResult[$index]["LINK"] <> "" && $index != $itemSize-1) {
$strReturn .= "<div class='breadcrumb__item' id='breadcrumb_".$index."' itemscope='' itemtype='".(CMain::IsHTTPS()? 'https' : 'http')."://data-vocabulary.org/Breadcrumb'".$child.$nextRef.">".$arrow."<a class='breadcrumb__link' href='".$arResult[$index]["LINK"]."' title='".$title."' itemprop='url'>".($index == 0 ? "<i class='fa fa-home breadcrumb__icon_main'></i>" : "")."<span class='".($index == 0 ? "breadcrumb__title_main" : "breadcrumb__title")."' itemprop='title'>".$title."</span></a></div>";
} else {
$strReturn .= "<div class='breadcrumb__item'>".$arrow.($index == 0 ? "<i class='fa fa-home breadcrumb__icon_main'></i>" : "")."<span class='".($index == 0 ? "breadcrumb__title_main" : "breadcrumb__title")."'>".$newtitle."</span></div>";
}
}
return $strReturn;
Ответы (2 шт):
Автор решения: Nickolay
→ Ссылка
Попробуйте сделать замену через функцию str_replace т.к то что вы пытаетесь заменит не является регулярным выражением.
<?if(!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED!==true)die();
if(empty($arResult))
return "";
$strReturn = "";
$itemSize = count($arResult);
for($index = 0; $index < $itemSize; $index++) {
$title = htmlspecialcharsex($arResult[$index]["TITLE"]);
$newtitle = str_replace(['[', ']'], ['<div style="display:none">', '</div'], $title);
$nextRef = ($index < $itemSize-2 && $arResult[$index+1]["LINK"] <> "" ? " itemref='breadcrumb_".($index + 1)."'" : "");
$child = ($index > 0 ? " itemprop='child'" : "");
$arrow = ($index > 0 ? "<span class='breadcrumb__arrow'></span>" : "");
if($arResult[$index]["LINK"] <> "" && $index != $itemSize-1) {
$strReturn .= "<div class='breadcrumb__item' id='breadcrumb_".$index."' itemscope='' itemtype='".(CMain::IsHTTPS()? 'https' : 'http')."://data-vocabulary.org/Breadcrumb'".$child.$nextRef.">".$arrow."<a class='breadcrumb__link' href='".$arResult[$index]["LINK"]."' title='".$title."' itemprop='url'>".($index == 0 ? "<i class='fa fa-home breadcrumb__icon_main'></i>" : "")."<span class='".($index == 0 ? "breadcrumb__title_main" : "breadcrumb__title")."' itemprop='title'>".$title."</span></a></div>";
} else {
$strReturn .= "<div class='breadcrumb__item'>".$arrow.($index == 0 ? "<i class='fa fa-home breadcrumb__icon_main'></i>" : "")."<span class='".($index == 0 ? "breadcrumb__title_main" : "breadcrumb__title")."'>".$newtitle."</span></div>";
}
}
return $strReturn;
Автор решения: Евгений Гаврилов
→ Ссылка
Можно попробовать через регулярное выражение
$str = 'Lenovo IdeaPad L340-15IRH 15.6"[(1920×1080)/Intel Core i5 9300H (2.4-4.1 ГГц)/8192/1000(5400)/128/NVidia GTX 1050(3 072)/DOS/нет/BT]';
$pattern = "#(\[[^\]]+\])$#";
$newStr = preg_replace($pattern, '<div style="display:none>"$1</div>', $str);
echo $newStr;
http://sandbox.onlinephpfunctions.com/code/ca99431f27e5d16b3d9ed8e9a910489e9a7782f2