修剪php的文本

时间:2016-02-03 02:12:46

标签: php chronoforms

我有chronoforms代码,每次有人填写表格时都会获得电子邮件的页面标题。但是,我总是使用额外的数据获取页面标题。

例如:

"Empodera tu ser con PNL - Fundación Empoder" 

我想删除

" - Fundación Empoder"

但我找不到答案

<input type='hidden' name='page_url' id='page_url' value='<?php echo \JURI::getInstance()->toString(); ?>' />

<?php
     $jdoc = \JFactory::getDocument();
?>
<input type='hidden' name='page_title' id='page_title' value='<?php echo $jdoc->getTitle(); ?>' />

如果您能提供帮助,请提前致谢:D

罗德里戈

3 个答案:

答案 0 :(得分:1)

Check out str_replace():

$text = 'Empodera tu ser con PNL - Fundación Empoder';
echo str_replace(' - Fundación Empoder', '', $text); // "Empodera tu ser con PNL"

This probably isn't the most elegant solution but, it'll certainly work if the part you want to remove is constant. Otherwise, you'll need to get creative and perhaps look into pattern matching/regex/etc.

答案 1 :(得分:1)

If you want to do something like this you need to make sure that there is always some pattern that you can look for. For example, if we know that there is only one dash then we can do something like this:

<?php
$title_pieces = explode("-", $jdoc->getTitle());
 $title_pieces = count($title_pieces > 1) ? 
   array_pop($title_pieces) : $title_pieces;
$page_title = implode('-', $title_pieces);
?>
<input type='hidden' name='page_title' id='page_title' value='<?php echo $page_title; ?>' />

I assume there is a better way of doing this, but if chronoforms doesn't allow this then this is a hacky way of doing it.

答案 2 :(得分:0)

感谢您帮助解决了这个问题

<input type='hidden' name='page_url' id='page_url' value='<?php echo \JURI::getInstance()->toString(); ?>' />

<?php
     $jdoc =  \JFactory::getDocument();
     $gettit =  $jdoc->getTitle();
     $alltrim = str_replace(' - Fundación Empoder', '', $gettit);
?>
<input type='hidden' name='page_title' id='page_title' value='<?php echo $alltrim; ?>' />

而且这是一个常数,这正是我所需要的......

感谢罗德里戈:D