0

Not sure if this has been asked but what's the best way to combine three strings into one string. I tried adding an array but it didn't work.

My strings are: $linktitle = get_the_title(); $linkt = substr($linktitle, 0, strpos($linktitle, ' —')); $linkt2 = substr($linktitle, 0, strpos($linktitle, ' –')); $linkt3 = substr($linktitle, 0, strpos($linktitle, ' |'));

Example $linktitle outputs:

Facebook Creates YouTube-Like Video Feature Inside Facebook | Re/code WNYC to Open New Podcast Division – The New York Times

My attempt at combination didn't work: $linkall = substr($linktitle, 0, strpos($linktitle, array(' —', –',' |')));

What I would like to accomplish is combine , –, and | into an array like in the above example (if possible).

4
  • If you're using an array, can use implode() to do this. Commented Oct 13, 2015 at 22:54
  • You're not making sense. Combine how? Explain more clearly what you want to achieve. Commented Oct 13, 2015 at 22:54
  • Please also include an example of what the content of $linktitle is. Commented Oct 13, 2015 at 22:56
  • @PedroLobito Clearly not since in the example the array is inside strops and has nothing to do with the three outputs. I can guess the attempt, but the question should be clear Commented Oct 13, 2015 at 23:05

2 Answers 2

1

How about using implode() on the array containing your strings?

Something like

$pieces = array($string1, $string2, $string3);
$result = implode('', $pieces);

In your case perhaps this makes more sense:

$pieces=array();
$pieces[] = substr($linktitle, 0, strpos($linktitle, ' —'));
$pieces[] = substr($linktitle, 0, strpos($linktitle, ' –'));
$pieces[] = substr($linktitle, 0, strpos($linktitle, ' |'));
$result = implode('', $pieces);
echo $result;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Solved problem.
0

Looks like, you have missed one quote before –

strpos($linktitle, array(' |', '–',' |')));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.