Closed Thread Icon

Preserved Topic: Show only first 50 characters of query (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=21045" title="Pages that link to Preserved Topic: Show only first 50 characters of query (Page 1 of 1)" rel="nofollow" >Preserved Topic: Show only first 50 characters of query <span class="small">(Page 1 of 1)</span>\

 
Bueromuenchen
Bipolar (III) Inmate

From: San Jose, CA
Insane since: Nov 2000

posted posted 10-23-2001 18:13

Hi guys,
who can tell my the mysql statement needed to retrieve the first 50 characters of a string in my database.

My query has already fetched the complete data, so i just need some help to only dispplay the first 50 characters in php.

propably i could also cut the string with js?

weird post, but you're gonna know what i need ...
thanks
Flo

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 10-23-2001 18:45

Virtually all languages have a set of string manipulation functions. PHP is no different

What you want is substr the format is this:
substr($string,0,49) remember couting usually starts at 0 so the first character is 0 the 50th is actually 49.

I'm not sure if and or how to do this in MySQL but generally it's easiest to use the SQL just to retrieve data and your language of choice to manupulate the returned data.

Here's the whole list of PHP string manipulation functions.




:[ Computers let you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila. ]:

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 10-23-2001 19:44

BD is right with substr, although that will chop words if it reaches the last character in the middle of the word. I'd love to see a simple solution that allows you to grab the first x words of a string....

Bueromuenchen
Bipolar (III) Inmate

From: San Jose, CA
Insane since: Nov 2000

posted posted 10-23-2001 20:02

thx bitdamaged,
works pretty fine.

pugzly, you´re right but
that's ok for me.

Flo


Emperor
Maniac (V) Mad Scientist with Finglongers

From: Cell 53, East Wing
Insane since: Jul 2001

posted posted 10-23-2001 20:11

Pugzly: Just off the top of my head I'd have thought you'd just explode the string using spaces - its not perfect but it would do the job if you only wanted 15 words say.

Emps


You're my wife now Dave

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 10-23-2001 21:54

If you use explode() to split words from very large text (i.e. big article from MySQL database), you'll end up with very large array that consume more memory than it's really necessary... This is how it should be done:

<?php

// Written by mr.maX, http://www.maxworld.co.yu/

function trimWords($text, $numWords) {

&nbsp;&nbsp;&nbsp;&nbsp;// Next statment removes multiple spaces (if any)
&nbsp;&nbsp;&nbsp;&nbsp;// BTW It can be removed if you're sure that words are separated with *one* space only!
&nbsp;&nbsp;&nbsp;&nbsp;$text = preg_replace("/[\x20]{2,}/", " ", $text);

&nbsp;&nbsp;&nbsp;&nbsp;// strtok() function is our friend! :-)
&nbsp;&nbsp;&nbsp;&nbsp;$tok = strtok($text, " ");
&nbsp;&nbsp;&nbsp;&nbsp;$i = 1;
&nbsp;&nbsp;&nbsp;&nbsp;$result = "";
&nbsp;&nbsp;&nbsp;&nbsp;while (is_string($tok) && $i <= $numWords) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$result .= "$tok ";
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$tok = strtok(" ");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$i++;
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;return rtrim($result);

}

?>




[This message has been edited by mr.maX (edited 10-23-2001).]

Emperor
Maniac (V) Mad Scientist with Finglongers

From: Cell 53, East Wing
Insane since: Jul 2001

posted posted 10-23-2001 22:17

mr.maX: Thanks - I was just thinking about checking to see if there was a better way of doing that when you posted your elegant solution (I don't know why I bother!!). I'll store that away for future reference (although substr has done me fine so far).

Emps


You're my wife now Dave

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 10-24-2001 00:00

Thanks for the info on strtok being less overhead mr. maX. I had a script that was using explode, but not any more.

Also mr. maX, is anyone else mentioning having problems accessing your site? I've tried quite a few times lately because I want to download the file to make Beauty do PHP. Every time I click on your sig I seem to get put in a session loop.

-Butcher-

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 10-24-2001 00:57

Ditto, Max. I just seem to slow down on each successive page. About the third or fourth page, and it's taking a minute or more to get to them.

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 10-24-2001 07:16

Butcher, I did notice some weird behavior with sessions and I'll try to fix that this weekend (or during next week).

Pugzly, I'll see what I can do about that (I think that in your case sessions are not the problem).


lallous
Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 10-24-2001 08:52

you can also use MySql's SUBSTRING(str

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 10-24-2001 09:14

Max could you toss a really good resource on reg expressions at me? I still have not gotten to learning those things, and I am sure it would really help me out if I did... It has been 3 years of doing this server side stuff and never learning any of the reg expression things.

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 10-25-2001 00:42

Hmmmm....I've been banging my head on the wall trying to get this to work and I must be missing something (other than a functional brain, of course).

I'm using:

$sql = "select * from articles where article = 228";
$myresult = mysql_query($sql) or die ("<b>SQL query failed: $sql</b><br>");

while ($row = mysql_fetch_array($myresult)) {

$text = $row[body];

print("<b>Body:</b> " . $text . "<br>\n");
print("<b>Trimmed body:</b> ");
// Written by mr.maX, http://www.maxworld.co.yu/

function trimWords($text, $numWords) {

// Next statment removes multiple spaces (if any)
// BTW It can be removed if you're sure that words are separated with *one* space only!
$text = preg_replace("/[\x20]{2,}/", " ", $text);

// strtok() function is our friend! :-)
$tok = strtok($text, " ");
$i = 1;
$result = "";
while (is_string($tok) && $i <= $numWords) {
$result .= "$tok ";
$tok = strtok(" ");
$i++;
}
return rtrim($result);
}
trimWords($row[body], 40);

}


mysql_free_result($result);
mysql_close($db);


It's getting the body from the database and displays it for the first part, but the "trimmed" part is empty. I've tried looking at the value of $i, and it's empty as well, leading me to believe that this function isn't running.

What's up with that?

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 10-25-2001 02:40

Pugzly

A couple of things:

[list]
*You need to remove the last } after the function call at the bottom of the script
*call $text in the function instead of $row[body]
* And I'm not sure why, but it seems to work if you echo the $result of the function instead of returning it
[\list]

Like this:

while (is_string($tok) && $i <= $numWords) {
$result .= "$tok ";
$tok = strtok(" ");
$i++;
}
echo rtrim($result);
}
trimWords($text, 40);

mysql_free_result($result);
mysql_close($db);


Also, the free_result and mysl_close threw me a not valid resource error, but I guess that's not a problem for you.

-Butcher-



[This message has been edited by butcher (edited 10-25-2001).]

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 10-25-2001 04:03

Actually, I can't remove the last bracket, since it closes the while ($row = mysql_fetch_array($myresult)) { line.

I called $test instead, after I defined it earlier in the script

I'm now printing the result instead of returning it. It's working with this code:


$sql = "select * from articles where article = 228";
$myresult = mysql_query($sql) or die ("<b>SQL query failed: $sql</b><br>");

while ($row = mysql_fetch_array($myresult)) {

print("<b>Body:</b> " . $row[body] . "<br>\n");
$text = $row[body];
print("<b>Trimmed body:</b> ");
// Written by mr.maX, http://www.maxworld.co.yu/

function trimWords($text, $numWords) {

// Next statment removes multiple spaces (if any)
// BTW It can be removed if you're sure that words are separated with *one* space only!
$text = preg_replace("/[\x20]{2,}/", " ", $text);

// strtok() function is our friend! :-)
$tok = strtok($text, " ");
$i = 1;
$result = "";
while (is_string($tok) && $i <= $numWords) {
$result .= "$tok ";
$tok = strtok(" ");
$i++;
}
print(rtrim($result));
}
trimWords($text, 40);

}


mysql_free_result($myresult);
mysql_close($db);


And I corrected the mysql_free_result line, so it should be better now.

The only anomoly is that it chops after 39 words instead of 40....no biggie, but interesting in itself.....

Thanks for the help.

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 10-25-2001 12:24

Sorry about the closing braket thing. I was pushing stuff around to test it locally before I posted back and inadvertantly threw a closing bracket in after the $text = $row[body] statement. While the code ran that way, it wouldn't have worked for more than one article.

I don't know about the 39 word thing, it gave me 40 when I ran it.

Is there a reason you use print instead of echo? I always use echo, and maybe I shouldn't be.

-Butcher-




[This message has been edited by butcher (edited 10-25-2001).]

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 10-25-2001 19:25

There's no need for modifying my function... It returns string which you can store in variable or print directly, so the only thing that you did wrong is that you didn't call print() function, like this:

print(trimWords($row[body], 40));

BTW You can also assign return value to variable (if you need it for some other things), like this:

$trimmedText = trimWords($row[body], 40);

And last, but not least, you should put my function outside while loop, since there's no need for it to be repeated...

Oh and I almost forgot that thing about 39 words, it should display 40 words, maybe you have an extra space at the beginning? You can easily fix this by calling ltrim() function...


Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 10-25-2001 22:06

Got it. I've optimized accordingly, and all is well.

Many thanks!

Bueromuenchen
Bipolar (III) Inmate

From: San Jose, CA
Insane since: Nov 2000

posted posted 10-27-2001 18:22

thanks dudes,
that's much more than i've expected,
but i can use it very well,

later
flo

« BackwardsOnwards »

Show Forum Drop Down Menu