Hello. Though I understand a little bit of php, but I cannot make one condition.

There is a variable $name which stores random values, on the basis of which I create a link (via echo )

<a href='/artist/".$name."'>".$name."</a>

When a single name is stored in a variable, for example Adel , then everything is fine, the link is formed with the correct <a href='/artist/Adel'>Adel</a>

But if there are several names in the variable, for example, Adel feat. Cher feat. 1GN Adel feat. Cher feat. 1GN Adel feat. Cher feat. 1GN then follow the link <a href='/artist/Adel feat. Cher feat. 1GN'>Adel feat. Cher feat. 1GN</a> <a href='/artist/Adel feat. Cher feat. 1GN'>Adel feat. Cher feat. 1GN</a> <a href='/artist/Adel feat. Cher feat. 1GN'>Adel feat. Cher feat. 1GN</a> will not work.

Now we go to the task itself, how to make the condition IF ELSE, so that there would be a check before creating the link. If the variable contains names with feat. then make a few links. 1 link - to feat., The second - after, etc.

What would happen on the similarity:

<a href='/artist/Adel'>Adel</a> feat. <a href='/artist/Cher'>Cher</a> feat. <a href='/artist/1GN'>1GN</a>

All the day I break my head ... I would be very grateful for any help !!

  • one
    explode() and an output loop to help you. - teran

1 answer 1

Check the $name line for the word " feat. "

 if(preg_match(" feat. ", $name)) { $namesArr = array(); //если нашлось $names = explode(" feat. ", $name);//то разбиваешь слова на массив foreach($names as $val) { $namesArr[] = "<a href=\"/artist/{$val}\">{$val}</a>"; } echo implode(" feat. ", $namesArr); // собираем строку взад } else { echo "<a href=\"/artist/{$name}\">{$name}</a>"; } 
  • one
    possible without verification. If there is no entry, then explode will return an array containing the source string. which is also derived using a single iteration of the loop - teran
  • Somehow you check the crooked string for the presence of the substring Warning: preg_match(): Delimiter must not be alphanumeric or backslash in ...
  • super, everything works! that's just, in preg_match search word needs to be written not feat. a /feat./ - Muday