使用由空格分隔的数据将变量中的数据放入
I am trying to load data from a variable that contains information and then place them into a string to use on web page using only the space as a divider. I know I have to loop through but I cannot. Shall I perform this in PHP before or use JS to do this? Also is it possible to load up to say 3 at a time? Is it possible to do this? The string looks like this :-
$areas = "London Birmingham Leeds Glasgow Bradford Liverpool Edinburgh etc etc"
and this is how I would like to place the data :-
<h3 class="areas">London</h3>
<h3 class="areas">Birmingham</h3>
<h3 class="areas">Leeds</h3>
or something like :-
<h3 class="areas">London Birmingham, Leeds</h3>
Thanks in advance for any advice.
我正在尝试从包含信息的变量加载数据,然后将它们放入要在网页上使用的字符串中 仅使用空格作为分隔符。 我知道我必须循环,但我不能。 我以前在PHP中执行此操作还是使用JS来执行此操作? 也可以一次加载3个? 是否有可能做到这一点? 字符串如下所示: - p>
$ areas =“London Birmingham Leeds Glasgow Bradford Liverpool Edinburgh等”
code> pre>
这就是我想要放置数据的方式: - p>
&lt; h3 class =“areas”&gt;伦敦&lt; / h3&gt;
&lt; h3 class = “areas”&gt; Birmingham&lt; / h3&gt;
&lt; h3 class =“areas”&gt; Leeds&lt; / h3&gt;
code> pre>
或类似名称: - p>
&lt; h3 class =“areas”&gt; London Birmingham,Leeds&lt; / h3&gt;
code> pre>
提前致谢 任何建议。 p>
div>
You do not need any JS to do that, only the explode
built-in function :
$areas_list = explode(' ', $areas);
foreach ($areas_list as $area) {
echo '<h3 class="areas">' . $area . '</h3>';
}
You can simply explode()
your String
into an Array
and loop through it :
$areas = "London Birmingham Leeds Glasgow Bradford Liverpool Edinburgh etc etc";
foreach(explode(' ',$areas) as $area){
echo '<h3 class="areas">'.$area.'</h3>';
}