PHP function - Shuffling / making a special word to always be the first in a string

Tom

I have a list of dozens of simple sentences/strings. Each sentence contains a special keyword. My task is to make sure this special keyword is ALWAYS as the first word of the sentence.

Let's assume my special keyword is: dog.

Example sentences BEFORE:

$string1='my dog is eating soup';
$string2='where have you seen my dog last time';
$string3='this little dog barks a lot';
$string4='most cats are better than my dog';

Sentences that should be AFTER:

dog my is eating soup
dog where have you seen my last time
dog this little barks a lot
dog most cats are better than my

I see there is str_shuffle PHP function but it shuffles words in a string randomly; I need to make sure that my special keyword (dog) is always first in sentence. And ideally this function should be as fast as possible since there are lots of such operations to complete.

NOTE - I only need an example on how to do it on one sentence, not all..

Rizier123

This should work for you:

Just simply remove the keyword in the string with str_replace() and append it at the start of the string, e.g.

<?php

    $str = "my dog is eating soup";
    $keyword = "dog";

    echo $newString = $keyword . " " . str_replace($keyword, "", $str);

?>

output:

dog my is eating soup

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Making first word/character of string in textview bigger

From Dev

Shuffling the first level of the array in PHP

From Dev

PHP only displaying first word of string in table

From Dev

Making string always be of length n

From Dev

String Split sql function only returning first word in string

From Dev

Issue with making word bold php

From Dev

Special string replace function

From Dev

Special string replace function

From Dev

PHP Regex — Remove all characters but first from last word in string

From Java

Get first word of string

From Dev

Removing first word in a string

From Dev

Why php gettype function always returns string from a $_POST[$var]?

From Dev

String passed into php function is breaking after first space in string

From Dev

Scan string for first occurrence of certain word and replace word plus next few characters with php

From Dev

Find and highlight word starting with Uppercase letter except the first word in a string using PHP?

From Dev

Making function for php condition statement

From Dev

Word VBA String insertion with special characters

From Dev

Find last word of a string that has special characters

From Dev

Word frequency in a string without spaces and with special characters?

From Dev

How to detect a special char in string and modify this special word?

From Dev

How to detect a special char in string and modify this special word?

From Dev

Problems with shuffling characters in a string

From Dev

Random shuffling of String array

From Dev

Print word containing string and first word

From Dev

How to always fetch a special category item data first in Laravel

From Dev

Select first word in string with jQuery

From Dev

matching first word from a string

From Dev

Get first word of string with sed

From Dev

swift - String: Shorten the first word

Related Related

  1. 1

    Making first word/character of string in textview bigger

  2. 2

    Shuffling the first level of the array in PHP

  3. 3

    PHP only displaying first word of string in table

  4. 4

    Making string always be of length n

  5. 5

    String Split sql function only returning first word in string

  6. 6

    Issue with making word bold php

  7. 7

    Special string replace function

  8. 8

    Special string replace function

  9. 9

    PHP Regex — Remove all characters but first from last word in string

  10. 10

    Get first word of string

  11. 11

    Removing first word in a string

  12. 12

    Why php gettype function always returns string from a $_POST[$var]?

  13. 13

    String passed into php function is breaking after first space in string

  14. 14

    Scan string for first occurrence of certain word and replace word plus next few characters with php

  15. 15

    Find and highlight word starting with Uppercase letter except the first word in a string using PHP?

  16. 16

    Making function for php condition statement

  17. 17

    Word VBA String insertion with special characters

  18. 18

    Find last word of a string that has special characters

  19. 19

    Word frequency in a string without spaces and with special characters?

  20. 20

    How to detect a special char in string and modify this special word?

  21. 21

    How to detect a special char in string and modify this special word?

  22. 22

    Problems with shuffling characters in a string

  23. 23

    Random shuffling of String array

  24. 24

    Print word containing string and first word

  25. 25

    How to always fetch a special category item data first in Laravel

  26. 26

    Select first word in string with jQuery

  27. 27

    matching first word from a string

  28. 28

    Get first word of string with sed

  29. 29

    swift - String: Shorten the first word

HotTag

Archive