简体   繁体   English

如何在php str_replace函数中加粗文本?

[英]How do i bold the text in a php str_replace function?

<?php
$xml = simplexml_load_file("xmldocumentation.xml") 
   or die("Error: Cannot create object");


 foreach($xml->children() as $pages){
   foreach($pages->children() as $page => $data){
   echo $data->id;
   echo '<br>';
   echo $data->timestamp;
   //echo $data->revision;
   echo "<br />";
   echo str_replace("/===[^=]+===/","bold heading here", $data->text);
   echo '<p class="text">'.$data->text.'</p>';

  }
}

?>  

Using php how do i bold the text replaced by php's str_replace function and display the modified content with bold headings ie. 使用php我如何将被php的str_replace函数替换的文本加粗并显示带有粗体标题的修改内容,即。 ' === Heading === '? ' ===标题=== '?

Thanks 谢谢

Change 更改

    echo str_replace("/===[^=]+===/","bold heading here", $data->text);

to

    $data->text = preg_replace("/===([^=])+===/","<strong>$1</strong>", $data->text);

You need preg_replace for regex, and need to capture the text you want to bold (via the () ), and then enclose that text in an HTML element that will give you the desired formatting. 您需要preg_replace用于正则表达式,并且需要捕获要加粗的文本(通过() ),然后将该文本包含在HTML元素中,以提供所需的格式。

A <b> </b> can do the trick! <b> </b>可以解决问题!

$strlst = 'lorem ipsum';

$strlst = explode( ' ' , $strlst );
function wrapTag($inVal){
   return '<b>'.$inVal.'</b>';
}
$replace = array_map( 'wrapTag' , $strlst );

$Content = str_replace( $strlst , $replace , $Content );

echo $Content;

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

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