Jump to content

Archived

This topic is now archived and is closed to further replies.

jerastraub

Splitting a string into several strings

Recommended Posts

Hello,

I have a question. Is there a way to split a string into several strings. Like:

offers cozy texture and a comfy full fit. Pleated at yoke for a cape-like sweep. Ribbed texture on the yoke and cuffs. Handy side-seam pockets. Full sleeves gathered at cuffs. Woven cotton/polyester. Machine wash. Imported. 25'' long. Sizes: M(14W-16W), L(18W-20W), 1X(22W-24W), 2X(26W-28W), 3X(30W-32W), 4X(34W-36W).

I want to split this string into 2 strings

First one would be:

offers cozy texture and a comfy full fit. Pleated at yoke for a cape-like sweep. Ribbed texture on the yoke and cuffs. Handy side-seam pockets. Full sleeves gathered at cuffs. Woven cotton/polyester. Machine wash. Imported. 25'' long.

second would be:

Sizes: M(14W-16W), L(18W-20W), 1X(22W-24W), 2X(26W-28W), 3X(30W-32W), 4X(34W-36W).

And then display them in an echo statement.

Share this post


Link to post
Share on other sites
[code]
<pre>
<?php

$string = "offers cozy texture and a comfy full fit. Pleated at yoke for a cape-like sweep. Ribbed texture on the yoke and cuffs. Handy side-seam pockets. Full sleeves gathered at cuffs. Woven cotton/polyester. Machine wash. Imported. 25'' long. Sizes: M(14W-16W), L(18W-20W), 1X(22W-24W), 2X(26W-28W), 3X(30W-32W), 4X(34W-36W).";
$pieces = preg_split('/(?=Sizes)/', $string);
print_r($pieces);

?>
</pre>

[/code]

Share this post


Link to post
Share on other sites
here's my ghetto solution:

[code]
$oldstring = "offers cozy texture and a comfy full fit. Pleated at yoke for a cape-like sweep. Ribbed texture on the yoke and cuffs. Handy side-seam pockets. Full sleeves gathered at cuffs. Woven cotton/polyester. Machine wash. Imported. 25'' long. Sizes: M(14W-16W), L(18W-20W), 1X(22W-24W), 2X(26W-28W), 3X(30W-32W), 4X(34W-36W).";

$d = 'Sizes:';
$strings = explode($d, $oldstring);
$strings[1] = $d . $strings[1];
echo "1st part: $strings[0] <br>";
echo "2nd part: $strings[1]";
[/code]

Share this post


Link to post
Share on other sites
Here's yet another way (YAW):
[code]<?php
$str = 'offers cozy texture and a comfy full fit. Pleated at yoke for a cape-like sweep. Ribbed texture on the yoke and cuffs. Handy ' .
        'side-seam pockets. Full sleeves gathered at cuffs. Woven cotton/polyester. Machine wash. Imported. 25" long. Sizes: ' .
        'M(14W-16W), L(18W-20W), 1X(22W-24W), 2X(26W-28W), 3X(30W-32W), 4X(34W-36W)';
$sp = strpos($str,'Size');
$str1 = substr($str,0,$sp);
$str2 = str_replace($str1,'',$str);
echo 'String  : ' . $str . "<br><br>\n";
echo 'String 1: ' . $str1 . "<br><br>\n";
echo 'String 2: ' . $str2 . "\n";
?>[/code]

Ken

Share this post


Link to post
Share on other sites
Nope not working :(

Here my code:

[code]<?
include("config.php");
// Get All Product Information Below For SE Optimizing
$sql = "SELECT ProductName,BriefDesc,prodPrice,IntermDesc,ProductURL,BigImage,PermuColor,prodSale,CategoryID,Category FROM womensproducts WHERE ProductID = '$pid'
";
$query = mysql_query($sql);
$viewproduct = mysql_fetch_array($query);
$active = mysql_num_rows(mysql_query($sql));

$oldstring = $viewproduct[3];

$d = 'sizes';
$strings = explode($d, $oldstring);
$strings[1] = $d . $strings[1];

$lowercolors = strtolower($viewproduct[6]);
$colors = ucwords($lowercolors);
$colors = ucwords($lowercolors);

?>[/code]

[code] <td class="quick" align=center><? echo $strings[0]; ?><br><br><? echo $viewproduct[3]; ?><br><br>
Avail. $strings[1]
<br><br>
<br>Avail. Colors: <? echo $colors; ?>[/code]

Am i missing something?

Share this post


Link to post
Share on other sites
Case of the string being searched for?
[code]<?php  $d = 'sizes'; ?>[/code]

In your original message you had 'Sizes'

Ken

Share this post


Link to post
Share on other sites
Okay,

Here's another one for ya:

The wording be the size can be

Size

or

Full and half sizes.

How do it get it do this.

I know I have to perform a test, but having trouble figuring it out!

Share this post


Link to post
Share on other sites
try
[code]
<?php
$s1 = 'Full and half sizes:';
$s2 = 'Sizes:';

$str = "offers cozy texture and a comfy full fit. Pleated at yoke for a cape-like sweep. Ribbed texture on the yoke and cuffs. Handy side-seam pockets. Full sleeves gathered at cuffs. Woven cotton/polyester. Machine wash. Imported. 25'' long. Full and half sizes: M(14W-16W), L(18W-20W), 1X(22W-24W), 2X(26W-28W), 3X(30W-32W), 4X(34W-36W).";

$split = (($p=strpos($str, $s1))===false) ? strpos($str,$s2) : $p;

$t1 = substr($str,0,$split);
$t2 = substr($str,$split);

echo '<p>' . $t1 . '</p><p>' . $t2 . '</p>';
?>[/code]

Edited to include ":" in search strings suggested by mjdamato below

Share this post


Link to post
Share on other sites
An even better solution would be to split the description and size information text into separate fileds within the database. At the very least you should do a check of your data to ensure that "Size" or "Full and half sizes" occurs at least once where appropriate and doesn't appear in the description - I could see where "Size" might be int eh description, so I would add the colon as part of the search value.

Share this post


Link to post
Share on other sites
I wish creating field in my database for this wouldn't be a very time consuming idea, as I use 12 datafeed on just this one site. These datafeeds are updated daily, so I upload them daily. If you compound that by 33 site in different nich markets. All my time would be spent modifying feeds rather than seo and customers service.

Share this post


Link to post
Share on other sites

×

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.