4

I have my working code which extracts the title from a string, but right now it still isn't very flexible.

Current code:

$post_title = "THIS IS A TEST - 10-01-2010 - HELLO WORLD (OKAY)!!";
$post_title = substr($post_title, 0, strpos($post_title, '-') - 1);

I want to get the title of the string, which is at the start of the string and goes until the first dash. I don't want to get the spaces before the dash and it could be that there is no dash at all.

The output of the current code works and outputs THIS IS A TEST, but the current code doesn't work for the following cases, so I need a more flexible code:

  • THIS IS A TEST - 10-01-2010 - HELLO WORLD (OKAY)!!
  • THIS IS A TEST-10-01-2010 - HELLO WORLD (OKAY)!!
  • THIS IS A TEST - - - - 10-01-2010 - HELLO WORLD (OKAY)!!
  • THIS IS A TEST

So the title can exist without a - and someone could forget to put a space between the -, equally, someone could put too many spaces.

The output for all the above cases should always be THIS IS A TEST with no spaces at the end.

With the code I have, the only one that works is the first one.

$title= explode('-', $post_title);
$post_title=trim($title[0]);
$trimmedTitle=$post_title;

$str = "THIS IS A TEST -0-01-2010 - HELLO WORLD (OKAY)!!";
preg_match("/^([\w\s]+)\s*-?/m", $str, $m);
print_r($m);
Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
Arthor
  • 666
  • 2
  • 13
  • 40
  • 1
    You probably want to use a simple regex for this. Put your example titles into https://regex101.com/ and use the quick reference at the bottom right to try something. – Rizier123 Jun 25 '16 at 20:09
  • @Rizier123 I wish I could Rizier123, I have spent a lot of time on this already and even more, my boss is breathing down my neck. The solution I have works but it creates a lot of problems as I have to manually fix the mistakes. This is one more great bastions of hope here at SOF. Working weekend :( – Arthor Jun 25 '16 at 20:13
  • @Anant Would it be like this `if(strpos($string,'$post_title')){echo explode('$post_title',$string)[0];}` I mught have failed to mention that `$post_title'` is the title of the wordpress post, thus it will change depending on the post. Thank you, I am testing it now. – Arthor Jun 25 '16 at 20:23
  • @Anant Case 2 does not work nor case 4. (Case 4 & 2 removes 1 letter) `THIS IS A TES` – Arthor Jun 25 '16 at 20:27
  • @Anant Sorry one moment. I and just working it out. Please, I think it was me. – Arthor Jun 25 '16 at 20:31
  • @Anant Sorry to sound stupid. But does your answer check the `function checkdata($string){ if(strpos($string,"THIS IS A TEST")!== false){ $returndata = 'THIS IS A TEST'; return $returndata;` do all that is need. if so, only by `strpos`. Sorry, i am most curious. – Arthor Jun 25 '16 at 21:11
  • And, would `$post_title` replace `THIS IS A TEST` – Arthor Jun 25 '16 at 21:12
  • I have tried this but it does not work: `$string1=$post_title; function checkdata($string){ if(strpos($string,$post_title)!== false){ $returndata = $post_title; return $returndata; } } echo "
    ";print_r(checkdata($string1));`  Just as a means of a test.
    – Arthor Jun 25 '16 at 21:22

2 Answers2

6

Use explode() with the "-" as a delimiter to split it into chunks based on the presence of the "-" and then take the first portion and use trim() to trim the trailing spaces to give the title with no trailing spaces. This gives "THIS IS A TEST" in all the provided test cases. If there are no dashes then the entire string is returned.

<?php

    $str = 'THIS IS A TEST - 10-01-2010 - HELLO WORLD (OKAY)!!';
    $title= explode('-', $str);
    $trimmedTitle=trim($title[0]);
    print_r($trimmedTitle);

    //$trimmedTitle ='THIS IS A TEST 

?>

I have tested this against:

THIS IS A TEST - 10-01-2010 - HELLO WORLD (OKAY)!!
THIS IS A TEST-10-01-2010 - HELLO WORLD (OKAY)!!
THIS IS A TEST - - - - 10-01-2010 - HELLO WORLD (OKAY)!!
THIS IS A TEST

and each returns THIS IS A TEST with no trailing spaces

gavgrif
  • 15,194
  • 2
  • 25
  • 27
  • 1
    happy to help - and the advantage of using this method is that you also have access to the rest of the information in the string - eg date of posting and content of post. just by accessing different portions of the $title array – gavgrif Jun 25 '16 at 23:27
  • 1
    @Arthor the above one will not work in the case if `THIS IS A TEST` is in middle part of the string for example:- `THIS IS WORKING -THIS IS A TEST - 10-01-2010 - HELLO WORLD (OKAY)!!` – Alive to die - Anant Jun 26 '16 at 08:31
  • 1
    @Anant - that is true - but OP stated the "I want to get the title of the string, which is at the start of the string and goes until the first dash." and so the idea was to get the first portion of the string since that is the position the title of the post. – gavgrif Jun 26 '16 at 09:05
  • 1
    @gavgrif i am not opposing you. I want to just tell him that if in any case in future position changed then some more work is needed. – Alive to die - Anant Jun 26 '16 at 09:07
0

If you are going to use explode() then it is best practice to limit the number of elements in the output array to 2 -- since you are only interested in the first element. Think about it, why bother exploding on any subsequent hyphens in the text if you aren't planning on using them?

$str = "THIS IS A TEST -0-01-2010 - HELLO WORLD (OKAY)!!";
$parts = explode($str, '-', 2);
$post_title = rtrim($parts[0]);

...but there are two things that I don't like about this technique:

  1. the input is a string and the output is a string, but you are populating an array along the way and
  2. look at how you have to go the extra step to mop up any lingering spaces at the end of the title

For this reason, I would recommend using preg_replace() to directly trim the unwanted characters without generating any arrays and without any extra mopping up.

Code: (PHP Demo) (Pattern Demo)

$str = "THIS IS A TEST -0-01-2010 - HELLO WORLD (OKAY)!!";
var_export(preg_replace('~ *-.*~', '', $str));

Output:

'THIS IS A TEST'

The pattern says, match zero or more spaces that are followed by the first occuring hyphen, then match zero or more characters until the end of the line/string.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136