Jump to content

Remove first four characters from title


Roych
 Share

Recommended Posts

Hello

I'm trying to remove first four characters from a title field (or custom text field), how would I do this?

I have titles like (they are imported with csv and numbers are for automatic categorising sort so they have to be there)

01 - Category1
02 - Category2
03 - Category3

I only want to output the Category1, category2, etc...

Any help appreciated

Thank you
R

Link to comment
Share on other sites

  • Roych changed the title to Remove first four characters from title

The PHP function substr() can be used to start after the 4. character. You can use it in your template file or create a little textformatter module.

(Sorry im on mobile, no link to the PHP manual provided.)

  • Like 2
Link to comment
Share on other sites

@horstbeat me to it ?.

Two options:

Option 1: substr() https://www.php.net/releases/8.0/en.php (+ trim() only if needed) edit.

<?php namespace ProcessWire;

$originalString = '01 - Category1';
// will remove the space after 01 - for you as well
$str = substr($originalString, 5); 
// debug
// d($str,'substr');

Option 2: array explode() + trim()

<?php namespace ProcessWire;

$originalString = '01 - Category1';
// explode the string on the hyphen ('-')
// this creates an array
$str2Array =  explode("-",$originalString);
// our string is in the second position
// we get it and remove spaces on each side
$str2 = trim($str2Array[1]);
// debug
// d($str2Array,'$str2Array');
// d($str2,'str2');

substr.thumb.png.e9c61a0de7481042bf9d076db79ef0c5.png

 

Edited by kongondo
typos
  • Like 2
Link to comment
Share on other sites

Hi,

At what time do you want to remove thosecharacters: when importing the data, when displaying the title on the frontend ?
depending on your need this could be achieed with a substr PHP function (when displaying the title on the frontend) or via a hook when importing/saving the title.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...