user3324395
user3324395

Reputation: 3

how to echo the words after - character

i have a page and its showing all the names with "-" character between them

and i want to just echo the first word before the - character and the second word after the - character

arsenal - liverpool

Betis - Athletic Bilbao

Waasland Beveren - Oostende

the code i tired

                        $arr1 = explode(' ',trim($Event["Event"]["name"]));

                        echo $arr1[0]."\n";

but this is just showing the first word , if the first word includes tow names then i cant echo it , i want to echo the full name before and after the - character in tow seprate php

i want to get this outputs from the examples

arsenal

liverpool

Betis

Athletic Bilbao

Waasland Beveren

Oostende

Upvotes: 0

Views: 128

Answers (2)

RJK
RJK

Reputation: 151

Try this:

$arr1 = explode('-', trim($Event["Event"]["name"]));

$team = $arr1[0];
$town = $arr1[1];

echo "$team - $town";

You are using the explode function with the wrong argument value, it should be used with a hyphen (-) instead of a space.

Note this will leave you with leading and trailing spaces, to remove this you can use the trim function:

$team = trim($arr1[0]);
$town = trim($arr1[1]);

Or put a space before and after the hyphen in the explode function:

$arr1 = explode(' - ', trim($Event["Event"]["name"]));

This is only the case if your string will always be in the format:

<team> - <town>

Upvotes: 2

Shital Marakana
Shital Marakana

Reputation: 2887

The explode() function breaks a string into an array. Note: The "separator" parameter cannot be an empty string. Use "-" separator in explode function.

<?php
$arr1 = explode('-',trim($Event["Event"]["name"]));

echo trim($arr1[0])."\n";
echo trim($arr1[1])."\n";
?>

Upvotes: 2

Related Questions