Jump to content

How do I break this variable up, into unlimited amounts?


simona6

Recommended Posts

Quote

#adf #234432 #fwerfw

I have this as a result of a form that has been completed.  It might be twice that, it might be much much more.

But what I want to do is render it on a page inside Divs.  So each one is in a kind of shaded box.

The CSS side of it is easy, but how do I extract each one of these into a variable useful means to surround each one by <div>$variable</div>?

I thought it might be "explode", but you appear to have to state the [3] number of the variable to show.  In this case, it's totally random.

We won't be using these as Variables to trigger anything else, it's purely "for display" purposes.

Link to comment
Share on other sites

I have the content in,  let's say $hastags.

I want to split that content so it starts at the #, and ends at the space.

I'm asking of the best way to do this - it might not be explode().  Trouble is, when I have seen that used, it's usually numbered, and if you don't know how many there will be, how do you know how many arrays to look at?

<?php
$mystr='I like Programming';
//space is used as delimiter
$parts=explode(' ', $mystr);
echo $parts[0] . '<br/>';
echo $parts[1] . '<br/>';
echo $parts[2];
?>

Here is an example.  But what if there are 15 terms in here to be split up.  How do you manage that, dynamically?  One might have 15, one might have just 2.

I can of course easily put them into variables, and the answer might be as a 'count'.

Edited by simona6
Link to comment
Share on other sites

Answered my own question....

//space is used as delimiter
$parts=explode(' ', $row->uniquehashtag);
echo count($parts);
echo $parts[0] . '<br/>';
echo $parts[1] . '<br/>';
echo $parts[2];

'count' shows how many there are, then I can put those [0] arrays into a loop by the amount of count.

Link to comment
Share on other sites

1 minute ago, Barand said:

try


$hashtags = '#adf #234432 #fwerfw';
$array = explode(' ', $hashtags);

foreach ($array as $var) {
    echo "<div style='width: 200px; border: 1px solid red; margin: 8px; padding: 8px'>$var</div>";
}

image.png.d7f0d24a481bdfa918de8d6ecc34dad3.png

Ooo I like that method too. :)

Link to comment
Share on other sites

$hashtags = strip_tags($row->uniquehashtag);
$array = explode(' ', $hashtags);
foreach ($array as $tag) {
    echo "<span class='gig-hashtags'>$tag</span>";
}

Spot on - removes the <p> tag in the code too!  Plus anything else they might have added.

Thanks everyone.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.