How do i do this eg:
echo " Hello world.How are you ?";
I want to seperate the strings eg output:
word1:Hello
word2:world
word3:How
word4:are
word5:you
Thanks Guys
Watch
GitHub Core
Show your support for Zikula! Sign up at Github account and watch the Core project!
GitHub Modules
- mesteele101 responded to »ERR (3): E_USER_ERROR: Smarty error: [in pagesvar:pagesitem2en line XXX]…« 07:01 AM
- mazdev responded to »Pages 2.5.0 and updating - Page not found« 06:41 AM
- ehdwma created topic »Hide "Register new account" and change template to 3 col« 06:27 AM
- mesteele101 responded to »Zikula 1.3.3 - Selecting a category in Pages not working« 01:29 AM
- mdee created topic »How to implement returnpage ?« 01:00 AM
- nestormateo responded to »Fillters in Clip« 24. May
- damon responded to »Can the Updated Version Check be Turned Off (Z 1.3)« 24. May
Zikula Blog
- Anatomy of Open Source Projects on Mar 07
- Continuous Review on Mar 01
- Not Invented Here on Feb 24
- How to Contribute Your Code at Github on Jan 13
- 10 Steps to Coding-Nirvana: Tips for Successful Module Writing on Nov 12
- Submitting Bug Report Tickets That Get Results on Aug 17
- Cozi Tricks #1: Syntax Highlighting on Aug 07
Login
Php strings help
-
**unknown user**
- Rank: Registered User
- Registered: Mar 16, 2002
- Last visit: Jul 19, 2005
- Posts: 16
I'm not sure what you mean. Do you want to know how you should echo strings in modules? If all you want to do is echo a single line the easiest way is to just make a function and use return, i.e:
Code
function Example_admin_main()
{
return "Hello world. How are you?";
}
But for more complex HTML you should look into using pnHTML or pnRender.
If you are asking how you would split up a string by using a common denominator, then there's is a PHP function called explode that you can use:
http://uk.php.net/explode
I.e:
Would write something like this to the screen:
[1] => Hello
[2] => world.How
[4] => are
[5] => you?
But as you can see this leaves in the punctuation marks which i'm guessing you don't want, you could try to remove them using str_replace but it could lead to problems with malformed sentences. Also explode will only take one common denominator, so as you see where you have the full stop and no space it isn't broken apart properly. I suspect someone must have written a function to disect sentences to a much better extent than the example I gave, but you only gave this as an example and so disection of sentences may not even be what you want. -
**unknown user**
- Rank: Registered User
- Registered: Mar 16, 2002
- Last visit: Oct 21, 2009
- Posts: 20
Yes thank you!!!
I manage to do that but i still need to seperate the string from puntuations,comas and fullstops.How do i do that?In other words i only want to extarct only words.
do i need to do this :
$string='hello world.How are you?';
$temp = Explode(" ", $string);
$temp2= Explode(", ", $temp);
$temp3 = Explode(" ?", $temp2);
Actually my objective is to pass the sepaerated words to a search function.
Eg:
Echo "Hello world. How are you?";
Output on web browser:
Hello world. How are you?
As you see,only the words got URL link.Thats wat i want to do.I only want to extract the words so that i can insert a URL.
Thank you ...i appreciate it -
- Rank: Developer
- Registered: Dec 31, 1969
- Last visit: Jun 01, 2010
- Posts: 6859
THis is really beyond the scope of these forums, you may be better served asking in a more nebulous PHP forum, like the PHP forum over at http://www.sitepoint.com
--
Home Page | Find on Facebook | Follow on Twitter
-
**unknown user**
- Rank: Registered User
- Registered: Mar 16, 2002
- Last visit: Jul 19, 2005
- Posts: 16
Well if you have a pretty recent version of PHP you can use the str_word_count function.
Code
Will output:
Quote
[ Hello | world | How | are | you ]
It would be quite easy to add links to that example, but as you see it looks slightly odd how the 'How's' first letter is capatalised, so you may want to consider using strtolower to make it uniform.
Of course chaning this example to use pnRender would look soemthing like:
Code
function Example_admin_main()
{
$pnRender =& new pnRender('pnLibrary');
$string = "Hello world.How are you?";
$words = str_word_count($string, 1);
$pnRender->assign('words', $words);
return $pnRender->fetch('Example_admin_showlinks.htm');
}
And then showlinks.htm:
Code
<center>[
<!--[foreach name=wordsloop from=$words item=word]-->
<!--[$word]-->
<!--[if $smarty.foreach.wordsloop.last == false]-->| <!--[/if]-->
<!--[/foreach]-->
]</center>
Of course if you have an older version of PHP you may not have str_word_count but you may want to look at the PHP page as some people have posted other ways of doing it with more common functions in the comments.
Alternativly building on the code I gave above, you could just use str_replace to remove all common punctuation first, then explode and strip out all the blank entries. I.e:
Code
$punc = array(".", ",", ";", ":", "(", ")", "!", "?", "&");
$string = "Hello world.How are you?";
$string = str_replace($punc, " ", $string);
$words = explode(" ", $string);
$words = array_filter($words);
- Moderated by:
- Support
