Jump to content


Photo

Splitting a string into several strings


  • Please log in to reply
9 replies to this topic

#1 jerastraub

jerastraub
  • Members
  • PipPipPip
  • Advanced Member
  • 77 posts

Posted 06 October 2006 - 04:39 PM

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.

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 06 October 2006 - 05:14 PM

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


Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 06 October 2006 - 05:16 PM

here's my ghetto solution:

$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]";

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 06 October 2006 - 05:59 PM

Here's yet another way (YAW):
<?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";
?>

Ken

#5 jerastraub

jerastraub
  • Members
  • PipPipPip
  • Advanced Member
  • 77 posts

Posted 06 October 2006 - 06:07 PM

Nope not working :(

Here my 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);

?>

<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; ?>

Am i missing something?



#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 06 October 2006 - 06:23 PM

Case of the string being searched for?
<?php  $d = 'sizes'; ?>

In your original message you had 'Sizes'

Ken

#7 jerastraub

jerastraub
  • Members
  • PipPipPip
  • Advanced Member
  • 77 posts

Posted 06 October 2006 - 06:38 PM

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!


#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 06 October 2006 - 06:53 PM

try
<?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>';
?>

Edited to include ":" in search strings suggested by mjdamato below
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#9 Psycho

Psycho
  • Moderators
  • Move along, nothing to see here
  • 11,892 posts
  • LocationCanada

Posted 06 October 2006 - 07:03 PM

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.
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#10 jerastraub

jerastraub
  • Members
  • PipPipPip
  • Advanced Member
  • 77 posts

Posted 06 October 2006 - 08:08 PM

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.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users