Trim a Text for a php

79 Views Asked by At

I have code in chronoforms to get the page title for an email each time someone fills the form. However, I'm always getting the page title with extra data.

For example:

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

I'd like to remove the

" - Fundación Empoder"

But I can't find an answer

<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(); ?>' />

Thanks in advance if you can help :D

Rodrigo

3

There are 3 best solutions below

0
Sean3z On BEST ANSWER

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.

0
Tim Hysniu On

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.

0
Rodrigo Zuluaga On

Thanks for the help you guys solved like this

<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; ?>' />

And yea its a constant, this removed exactly what I needed...

Thanks Rodrigo :D