简体   繁体   English

使PHP str_replace与Joomla一起使用

[英]Getting PHP str_replace to work with Joomla

As you may know, Joomla components enable you to override their output by copying their template files into your site template. 如您所知,Joomla组件使您可以通过将其模板文件复制到站点模板中来覆盖其输出。 Joomla components generally use helper files which cannot be overridden. Joomla组件通常使用无法覆盖的帮助文件。

I have a helper.php file that includes the string: 我有一个包含字符串的helper.php文件:

$specific_fields_text = '<tr><td class="key">'.$specific_field_title.': </td><td class="kr_sidecol_subaddress">'.$specific_fields[$i]->text.' '.$specific_fields[$i]->description.'</td></tr>';

In my template override is the code: 在我的模板中是代码:

<table border="0" cellpadding="2" cellspacing="0">
  <?php echo koparentHTML::getHTMLSpecificFields($this->specific_fields); ?>
</table>

The output is as follows: 输出如下:

<table border="0" cellpadding="2" cellspacing="0">
  <tr>
    <td class="key">title</td>
    <td class="kr_sidecol_subaddress">value</td>
  </tr>
  <tr>
    <td class="key">title</td>
    <td class="kr_sidecol_subaddress">value</td>
  </tr>
    //.....etc......//
</table>

Basically I want to get rid of the table and turn it into a definition list but I cannot modify the helper.php file. 基本上,我想摆脱该表并将其变成定义列表,但是我无法修改helper.php文件。 I am thinking that the answer is to do with str_replace 我认为答案与str_replace有关

I have tried using: 我试过使用:

<dl>
  <?php
    $spec_fields = koparentHTML::getHTMLSpecificFields($this->specific_fields);
    $spec_fields_dl = str_replace("<tr><td class='key'>'.$specific_field_title.': </td><td class='kr_sidecol_subaddress'>'.$specific_fields[$i]->text.' '.$specific_fields[$i]->description.'</td></tr>'", "<dt class='key'>'.$specific_field_title.': </dt><dd class='kr_sidecol_subaddress'>'.$specific_fields[$i]->text.' '.$specific_fields[$i]->description.'</dd>'", $spec_fields);
    echo $spec_fields_dl;
  ?>
</dl>

This returns all of the text but with no html tags (no tr, td, dt, etc). 这将返回所有文本,但没有html标记(没有tr,td,dt等)。

You can easily parse table data with PHP, like in this example: 您可以使用PHP轻松解析表数据,例如以下示例:

$doc = new DOMDocument();
$doc->loadHTML(koparentHTML::getHTMLSpecificFields($this->specific_fields));

$rows = $doc->getElementsByTagName('tr');

$data = array();
for ($i = 0; $i < $rows->length; $i++) {
    $cols = $rows->item($i)->getElementsbyTagName("td");
    $data[$cols->item(0)->nodeValue] = $data[$cols->item(1)->nodeValue];
}

var_dump $data;

This should convert your table into assoc array ('title' => 'value'). 这应该将您的表转换为assoc数组('title'=>'value')。 I hope it helps. 希望对您有所帮助。

I have figured this out. 我已经弄清楚了。 For some reason the PHP bits such as '.$specific_field_title.' 由于某些原因,PHP位例如'.$specific_field_title.' where stopping the str_replace from working. 在哪里停止str_replace的工作。 To get around this I just searched for the HTML elements and put them in an array like so: 为了解决这个问题,我只是搜索了HTML元素,并将它们放入数组中,如下所示:

echo str_replace(array('<tr><td class="key">', '</td><td class="kr_sidecol_subaddress">', '</td></tr>'),
array('<dt class="key">', '</dt><dd class="kr_sidecol_subaddress">', '</dd>'),
koparentHTML::getHTMLSpecificFields($this->specific_fields));

And now this works perfectly. 现在,这很完美。 Thank you to everyone who contributed. 感谢所有贡献者。

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

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