Jump to content

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.
Link to comment
https://forums.phpfreaks.com/topic/23192-splitting-a-string-into-several-strings/
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]
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]
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
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?

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
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.
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.

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.