PHP Code flipper - cleaner version needed

Started by The Wizard, September 24, 2014, 05:53:32 AM

Previous topic - Next topic

The Wizard

Hello:

I am working on a episode guide and need some help making my php script work more efficiently

What this script does:
takes "Wayne John,  McLintock," and flips it around to " McLintock as John Wayne,".

What I need help on:
The script works, but as you can see it's written badly. I need to make a for loop to replace this -

echo $parts[1],' as ',  $parts[0],',<br>';
echo $parts[3],' as ',  $parts[2],',<br>';
        etc.....

But I just can't seem to figure out the correct math.

Here is a example of the raw data -

QuoteEpisode 1 (53)

Cast: Caleb Ross as Lex, Dwayne Cameron as Bray, Meryl Cassie as Ebony, Michelle Ang as Tai-San, Victoria Spence as Salene, Ryan Runciman as Ryan, Michael Wesley Smith as Jack, Ashwath Sundarasen as Dal, Ella Wilks as Danni, Jennyfer Jewell as Ellie, Antonia Prebble as Trudy, Vanessa Stacey as Alice, Ari Boyland as KC, Sarah Major as Patsy, Jaimee Kaire-Gataulu as Cloe, Lee Donaghue as Spike, Josephine Byrne-Hay as Sick Kid No. 1, Lyndon Keenan as Sick Kid No. 2

Episode 2 (54)

Cast: Caleb Ross as Lex, Dwayne Cameron as Bray, Meryl Cassie as Ebony, Michelle Ang as Tai-San, Victoria Spence as Salene, Ryan Runciman as Ryan, Michael Wesley Smith as Jack, Ashwath Sundarasen as Dal, Ella Wilks as Danni, Jennyfer Jewell as Ellie, Antonia Prebble as Trudy, Vanessa Stacey as Alice, Ari Boyland as KC, Sarah Major as Patsy, Jaimee Kaire-Gataulu as Cloe, Sam Husson as Top Hat, Story Rose as Brady, Josephine Byrne-Hay as Sick Kid No. 1,

I would love to figure out a way to just dump the raw data into the script and have it do all the work as I have 4 more seasons to go and each season is 52 episodes long. I expect there is a way to make the "episode number" and "Cast:" exempt or ignored so it they will not throw off the script. Please note I have been using Notepad++ to search and replace the "as" to a "," before I install in the script. As you can imagine this is the long way around of doing things, but it's the best I can come up with. So if you can help please, please help.

Thanks

The Wizard 




Below is the php code to my code flipper.





<?php

$address  '
Caleb Ross , Lex, Dwayne Cameron , Bray, Meryl Cassie , Ebony, Michelle Ang , Tai-San, Victoria Spence , Salene, Ryan Runciman , Ryan, Michael Wesley Smith , Jack, Ashwath Sundarasen , Dal, Ella Wilks , Danni, Jennyfer Jewell , Ellie, Antonia Prebble , Trudy, Vanessa Stacey , Alice, Ari Boyland , KC, Sarah Major , Patsy, Jaimee Kaire-Gataulu , Cloe, Lee Donaghue , Spike, Story Rose , Brady, Damon Andrew , the Guardian, George Henare , President
'
;//create an array with your data

$parts explode(','$address);
$new_string $parts[1] . $parts[0];

echo $parts[1],' as ',  $parts[0],',<br>';
echo $parts[3],' as ',  $parts[2],',<br>';
echo $parts[5],' as ',  $parts[4],',<br>';
echo $parts[7],' as ',  $parts[6],',<br>';
echo $parts[9],' as ',  $parts[8],',<br>';
echo $parts[11],' as ',  $parts[10],',<br>';
echo $parts[13],' as ',  $parts[12],',<br>';
echo $parts[15],' as ',  $parts[14],',<br>';
echo $parts[17],' as ',  $parts[16],',<br>';
echo $parts[19],' as ',  $parts[18],',<br>';
echo $parts[21],' as ',  $parts[20],',<br>';
echo $parts[23],' as ',  $parts[22],',<br>';
echo $parts[25],' as ',  $parts[24],',<br>';
echo $parts[27],' as ',  $parts[26],',<br>';
echo $parts[29],' as ',  $parts[28],',<br>';
echo $parts[31],' as ',  $parts[30],',<br>';
echo $parts[33],' as ',  $parts[32],',<br>';
echo $parts[35],' as ',  $parts[34],',<br>';
echo $parts[37],' as ',  $parts[36],',<br>';
echo $parts[39],' as ',  $parts[38],',<br>';
echo $parts[41],' as ',  $parts[40],',<br>';
echo $parts[43],' as ',  $parts[42],',<br>';
echo $parts[45],' as ',  $parts[44],',<br>';
echo $parts[47],' as ',  $parts[46],',<br>';
echo $parts[49],' as ',  $parts[48],',<br>';
echo $parts[51],' as ',  $parts[50],',<br>';
echo $parts[53],' as ',  $parts[52],',<br>';
echo $parts[55],' as ',  $parts[54],',<br>';
echo $parts[57],' as ',  $parts[56],',<br>';
echo $parts[59],' as ',  $parts[58],',<br>';
echo $parts[61],' as ',  $parts[60],',<br>';
echo $parts[63],' as ',  $parts[62],',<br>';
echo $parts[65],' as ',  $parts[64],',<br>';
echo $parts[67],' as ',  $parts[66],',<br>';
echo $parts[69],' as ',  $parts[68],',<br>';
echo $parts[71],' as ',  $parts[70],',<br>';
echo $parts[73],' as ',  $parts[72],',<br>';
echo $parts[75],' as ',  $parts[74],',<br>';
echo '======= End =========================';

   
   

onepiece

This should do it I think:

$address = 'raw data goes here';
echo preg_replace('~(?:^| )([A-Za-z0-9\.\- ]+?) , ([A-Za-z0-9\.\- ]+)(,|$)~', '\\2 as \\1\\3 ', trim($address));

Kindred

maybe this might work...


untested


<?php

$names  
'Caleb Ross, Lex, Dwayne Cameron, Bray, Meryl Cassie, Ebony, Michelle Ang, Tai-San, Victoria Spence, Salene, Ryan Runciman, Ryan, Michael Wesley Smith, Jack, Ashwath Sundarasen, Dal, Ella Wilks, Danni, Jennyfer Jewell, Ellie, Antonia Prebble, Trudy, Vanessa Stacey, Alice, Ari Boyland, KC, Sarah Major, Patsy, Jaimee Kaire-Gataulu, Cloe, Lee Donaghue, Spike, Story Rose, Brady, Damon Andrew, the Guardian, George Henare, President'//create an array with your data
$parts explode(','$address);
 
i=0;
 
foreach (
$parts as $part) {
j=i+1;
echo $part[.$i.],' as ',  $part[.$j.],',<br />';
i++;
}
echo '======= End =========================';

?>

Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

onepiece

That wouldn't work. If you really want to do it with a loop, you'll need something like this:

$address = 'raw data goes here';
$parts = explode(',', trim($address));
$data = array();
for ($i = 0; $i < count($parts); $i += 2)
$data[] = trim($parts[$i + 1]) . ' as ' . trim($parts[$i]);
echo implode(',<br />', $data);


Also, if you want to use my first suggestion but want the names to be each on a new line, it's simple:

$address = 'raw data goes here';
echo preg_replace('~(?:^| )([A-Za-z0-9\.\- ]+?) , ([A-Za-z0-9\.\- ]+)(,|$)~', '\\2 as \\1\\3<br />', trim($address));

The Wizard


Thank you onepiece that worked great!

Now I need to finish off the script and save me a ton of work. As you can see from the script below I have added a few things -

1. A way to remove "Episode 1', 'Episode 2', 'Cast:" - Works
2. I used sort to alphabetize the list - works

3. I added the "*" at the end of each cast listing to create a return to separate the list  - does not work.

I have ruffed out a if statement for the return, but I do not know how to finish it.

4. I added the "Episode" back to each cast listing at the end of the script so I know what episode goes with each cast list, but I am unsure how to create the counting system to add the number. - does not work.

Any help on these issues would be appreciated

Thanks

The Wizard




$myString = '
Episode 1
Cast: Caleb Ross , Lex, Dwayne Cameron , Bray, Meryl Cassie , Ebony, Michelle Ang , Tai-San, Victoria Spence , Salene, Ryan Runciman , Ryan, Michael Wesley Smith , Jack, Ashwath Sundarasen , Dal, Ella Wilks , Danni, Jennyfer Jewell , Ellie, Antonia Prebble , Trudy, Vanessa Stacey , Alice, Ari Boyland , KC, Sarah Major , Patsy, Jaimee Kaire-Gataulu , Cloe, Lee Donaghue , Spike, Josephine Byrne-Hay , Sick Kid No. 1, Lyndon Keenan , Sick Kid No. 2*
Episode 2
Cast: Caleb Ross , Lex, Dwayne Cameron , Bray, Meryl Cassie , Ebony, Michelle Ang , Tai-San, Victoria Spence , Salene, Ryan Runciman , Ryan, Michael Wesley Smith , Jack, Ashwath Sundarasen , Dal, Ella Wilks , Danni, Jennyfer Jewell , Ellie, Antonia Prebble , Trudy, Vanessa Stacey , Alice, Ari Boyland , KC, Sarah Major , Patsy, Jaimee Kaire-Gataulu , Cloe, Sam Husson , Top Hat, Story Rose , Brady, Josephine Byrne-Hay , Sick Kid No. 1*
';

$words_to_remove = array('Episode 1', 'Episode 2', 'Cast:');

$myString = str_replace($words_to_remove,"",$myString);

$parts = explode(',', trim($myString));
$data = array();
for ($i = 0; $i < count($parts); $i += 2)
{
$data[] = trim($parts[$i + 1]) . ' as ' . trim($parts[$i]);
sort($data);

if ($data = *)
{
$data = $data =
}
}

$episode_number = 8;

echo 'Episode ', $episode_number, '<br /><br />';
echo implode(',<br />', $data);

The Wizard


Everything works on the code below. There is one flaw at the end it is says "Episode 3" and "as".
Anybody have a idea how to fix that? Remember I have to add 52 episodes of cast data.

Again thanks for all the help

The Wizard



Example Output:

QuoteNew Episode 1

Alice as Vanessa Stacey,
Bray as Dwayne Cameron,
Cloe as Jaimee Kaire-Gataulu,
Dal as Ashwath Sundarasen,
Danni as Ella Wilks,
Ebony as Meryl Cassie,
Ellie as Jennyfer Jewell,
Jack as Michael Wesley Smith,
KC as Ari Boyland,
Lex as Caleb Ross,
Patsy as Sarah Major,
Ryan as Ryan Runciman,
Salene as Victoria Spence,
Sick Kid No. 1 as Josephine Byrne-Hay,
Sick Kid No. 2 as Lyndon Keenan,
Spike as Lee Donaghue,
Tai-San as Michelle Ang,
Trudy as Antonia Prebble

New Episode 2

Alice as Vanessa Stacey,
Brady as Story Rose,
Bray as Dwayne Cameron,
Cloe as Jaimee Kaire-Gataulu,
Dal as Ashwath Sundarasen,
Danni as Ella Wilks,
Ebony as Meryl Cassie,
Ellie as Jennyfer Jewell,
Jack as Michael Wesley Smith,
KC as Ari Boyland,
Lex as Caleb Ross,
Patsy as Sarah Major,
Ryan as Ryan Runciman,
Salene as Victoria Spence,
Sick Kid No. 1 as Josephine Byrne-Hay,
Tai-San as Michelle Ang,
Top Hat as Sam Husson,
Trudy as Antonia Prebble

New Episode 3

as

Latest php Script


$myString = '
Episode 1
Cast: Caleb Ross , Lex, Dwayne Cameron , Bray, Meryl Cassie , Ebony, Michelle Ang , Tai-San, Victoria Spence , Salene, Ryan Runciman , Ryan, Michael Wesley Smith , Jack, Ashwath Sundarasen , Dal, Ella Wilks , Danni, Jennyfer Jewell , Ellie, Antonia Prebble , Trudy, Vanessa Stacey , Alice, Ari Boyland , KC, Sarah Major , Patsy, Jaimee Kaire-Gataulu , Cloe, Lee Donaghue , Spike, Josephine Byrne-Hay , Sick Kid No. 1, Lyndon Keenan , Sick Kid No. 2
Episode 2
Cast: Caleb Ross , Lex, Dwayne Cameron , Bray, Meryl Cassie , Ebony, Michelle Ang , Tai-San, Victoria Spence , Salene, Ryan Runciman , Ryan, Michael Wesley Smith , Jack, Ashwath Sundarasen , Dal, Ella Wilks , Danni, Jennyfer Jewell , Ellie, Antonia Prebble , Trudy, Vanessa Stacey , Alice, Ari Boyland , KC, Sarah Major , Patsy, Jaimee Kaire-Gataulu , Cloe, Sam Husson , Top Hat, Story Rose , Brady, Josephine Byrne-Hay , Sick Kid No. 1
';

$words_to_remove = array('Episode 1', 'Cast:');

$myString = str_replace($words_to_remove,"",$myString);


for ($i=0; $i<=2; $i++)
{
$split_string = explode('Episode '.$i, $myString);
}

$ep = 1;

for ($x=0; $x<=2; $x++)
{
$parts = explode(',', trim($split_string[$x]));
$data = array();
for ($i = 0; $i < count($parts); $i += 2)
{
$data[] = trim($parts[$i + 1]) . ' as ' . trim($parts[$i]);
sort($data);
}

echo '<br /><br />New Episode ',$ep,'<br /><br />';
echo implode(',<br />', $data);

$ep++;

}

onepiece

Change $x<=2 to $x<2 and it should work properly. You could also do this though, which will do the calculation for you:

$myString = '
Episode 1
Cast: Caleb Ross , Lex, Dwayne Cameron , Bray, Meryl Cassie , Ebony, Michelle Ang , Tai-San, Victoria Spence , Salene, Ryan Runciman , Ryan, Michael Wesley Smith , Jack, Ashwath Sundarasen , Dal, Ella Wilks , Danni, Jennyfer Jewell , Ellie, Antonia Prebble , Trudy, Vanessa Stacey , Alice, Ari Boyland , KC, Sarah Major , Patsy, Jaimee Kaire-Gataulu , Cloe, Lee Donaghue , Spike, Josephine Byrne-Hay , Sick Kid No. 1, Lyndon Keenan , Sick Kid No. 2
Episode 2
Cast: Caleb Ross , Lex, Dwayne Cameron , Bray, Meryl Cassie , Ebony, Michelle Ang , Tai-San, Victoria Spence , Salene, Ryan Runciman , Ryan, Michael Wesley Smith , Jack, Ashwath Sundarasen , Dal, Ella Wilks , Danni, Jennyfer Jewell , Ellie, Antonia Prebble , Trudy, Vanessa Stacey , Alice, Ari Boyland , KC, Sarah Major , Patsy, Jaimee Kaire-Gataulu , Cloe, Sam Husson , Top Hat, Story Rose , Brady, Josephine Byrne-Hay , Sick Kid No. 1
';

preg_match_all('~Episode \d+\s+Cast: (.+)~', $myString, $matches);
foreach ($matches[1] as $key => $value)
{
$parts = explode(',', trim($value));
$data = array();
for ($i = 0; $i < count($parts); $i += 2)
{
$data[] = trim($parts[$i + 1]) . ' as ' . trim($parts[$i]);
sort($data);
}
echo '<br /><br />New Episode ', $key + 1,'<br /><br />';
echo implode(',<br />', $data);
}

The Wizard

quick question - How would I echo the data in two columns?

Thanks


onepiece

This should work:

preg_match_all('~Episode \d+\s+Cast: (.+)~', $myString, $matches);
foreach ($matches[1] as $key => $value)
{
$parts = explode(',', trim($value));
$data = array();
for ($i = 0; $i < count($parts); $i += 2)
{
$data[] = trim($parts[$i + 1]) . ' as ' . trim($parts[$i]);
sort($data);
}
if ($key % 2 == 0)
echo '<div style="overflow: auto;">';
echo '<div style="float: ', (($key % 2 == 0) ? 'left' : 'right'), '; width: 49%;">';
echo '<br /><br />New Episode ', $key + 1,'<br /><br />';
echo implode(',<br />', $data);
echo '</div>';
if ($key % 2 != 0 || $key == count($matches[1]) - 1)
echo '</div>';
}

Advertisement: