Jump to content

Can You Have More Than One Delimiter/Combiner In A Function ?


phpsane

Recommended Posts

Php Gurus,

 

Is there a way how you could have more than one delimiter in the following functions ?

 

explode

implode

 

Tutorials always shows one. But let us say, we need more than one. In that case, what would you do without coding separately one by one for each delimiter ? If regex then I'd like to see some code samples.

Remember, a code sample would be appreciated by all present & future newbies. :)

 

 

 

Explode

Imagine I want to explode this:

 

'The "quick brown" fox jumps over the \'lazy dog\'.'

 

To this:

 

'

The 

"quick brown"

fox 

jumps 

over 

the 

\'lazy dog\'

.

'

 

As you can see, we need 3 delimiters:

 

Space

"

'

 

Now, imagine I need to implode this:

 

"


The 

'quick brown'

fox 

jumps 

over 

the 

\"lazy dog\"

.

"

 

To this:

 


"The 'quick brown' fox jumps over the \"lazy dog\"."


 

As you can see the combiners are the same in this case like they were in the other case's delimiters.


 

I don't want to be coding 3 separate snippets to use each delimiter/combiner. Hence the question.

Imagine, I got millions of html files and I need to deal with millions of lines of content like the examples shown above (which was just one line each).

 

Ok, I was about to post this now and a thought just crossed my mind. Maybe the delimiters/combiners can be placed in an array and then on each loop the delimiter/combiner can change ? But how do I call the delimiters/combiners from each array key ? Dump the array values to a variable ? Any sample would be appreciated. Saying all this, I will try myself to build 2 snippets from one of the codes from one of my previous threads but don't hold your breath and I prefer your feed-back if it's possible to do things with array like I'm pondering.

 

@requinix,

 

Yes, I am creating my own terminology again: Combiner/Joiner. ;)

Link to comment
Share on other sites

No.

 

Thanks.

Now let's try another method so we don;t have to code 3 lots of snippets for 3 lots of delimiters.

Can the delimiters/combiners be placed in an array and then on each loop the delimiter/combiner change ? If this is not bad method then I may attempt it. Just because it might work, it does not mean it is a good and strong method. Hence, the question to the experienced.

Link to comment
Share on other sites

Can the delimiters/combiners be placed in an array and then on each loop the delimiter/combiner change ?

What I think you're trying to get at, no you cannot do that. It's the same delimiter for the entire duration of the "loop".

 

There is a function that can do what you want. The documentation for explode() links to it. I'm sure there are user comments that talk about it too. I'm just waiting for you to see it.

Link to comment
Share on other sites

---

 

-Requinix,

 

I found it. I did- read- the manual fe-w days ago but did not read the user -codes as they -are- usu-ally- complicated and put a beginner like me OFF.

Today was going through the following like 2 days ago:

 

http://www.tizag.com/phpT/php-string-explode.php

https://www.tutorialrepublic.com/faq/how-to-split-a-string-into-an-array-in-php.php

https://www.tutorialspoint.com/php/php_function_explode.htm

 

And not the manual.

 

Anyway, to all newbies, here's the code Requinix was referring to:

<?php

//$delimiters has to be array
//$string has to be array

function multiexplode ($delimiters,$string) {
    
    $ready = str_replace($delimiters, $delimiters[0], $string);
    $launch = explode($delimiters[0], $ready);
    return  $launch;
}

$text = "here is a sample: this text, and this will be exploded. this also | this one too :)";
$exploded = multiexplode(array(",",".","|",":"),$text);

print_r($exploded);

//And output will be like this:
// Array
// (
//    [0] => here is a sample
//    [1] =>  this text
//    [2] =>  and this will be exploded
//    [3] =>  this also 
//    [4] =>  this one too 
//    [5] => )
// )

?>

On thing strange though! As  soo-n as I copied that code, my computer started acting weird and keeps typing on auto:

 

--------

 

It has stopped now. Was having a real big trouble trying to write this post.

 

Damn Win 10 trying to hinder my php learning!

 

The other day, this key did not work: >

Now, it is.

Sometimes keys don't work for a few days. And sometimes the computer keeps auto typing characters like today. Googled few- mnths -ago w-hen I first- ex-perienced thes-e and found lots- of people having is-s-ues- but none of the s-olutions -wo-rked for me.

Mentioning all this- s-o you guys- don't w-onder w-hy my pos-ts- are mes-s-y like you s-ee now-. I'm getting really pis-s-ed off at that Bill goat now-!

Link to comment
Share on other sites

Besides obvious water/liquid damage to the keyboard being the cause, I've also heard of mouse drivers (Logitech, I think) conflicting with the keyboard. Mostly with laptops.

 

Anyway, to all newbies, here's the code Requinix was referring to:

It is not.
Link to comment
Share on other sites

Besides obvious water/liquid damage to the keyboard being the cause, I've also heard of mouse drivers (Logitech, I think) conflicting with the keyboard. Mostly with laptops.

 

It is not.

 

S-o, that is- not the code you w-ere referring to ? Then which one ? Anyway, the code is- ok to learn. Is- it not ?

Any chance you w-ere hinting about:

http://php.net/manual/en/function.strtok.php

 

This- time round, I am noticing the the --- happens- when I type the S---or the w--or the x---. And you are 100% s-pot on about Logitec and laptops.

Tes-ting:

qw----ertyuiopas---dfghjklzx-cx----cvbnm.

I'll try updating driver. In the meanw-hile, if anyone els-e ex-perienced this- and has- a s-olution then let us- know--.

 

Thanks-------

 

 

EDIT

Found another is-s-ue. Right clicking mous-e resu-lts -in sc-reen flas--hing changing colours---. W-Hat a nuis-ance! 

Link to comment
Share on other sites

I like strtok. It's easy to use. But it's not helpful here: while it supports multiple delimiters, it doesn't return those delimiters. I'm talking about a function based on regular expressions, which supports multiple delimiters (because it uses a regex) and also supports returning those delimiters in the split array (which functions like explode() do not).

Link to comment
Share on other sites

Do you have a real purpose behind this question or are you just chasing rainbows because you are a 'newbie'?

 

 


I found it. I did read the manual few days ago but did not read the user codes as they are usually- complicated and put a beginner like me OFF.

 

(What are the 'user codes'?)

 

If you can't manage to read manuals because they are 'complicated', then perhaps you should find a new hobby.  As I've said before, the entire computer-related business is NOTHING BUT technicalities.  Hardware, software - they are both complex.  Sounds like being 'complex' is not your thing.  Perhaps that's why we get the big bucks - because we can read and understand normal English despite the topic.

 

There is such a thing as practice too.  Do you think the first manual we've all had to read was all about Dick & Jane?

 

Good luck in your future endeavors.

Link to comment
Share on other sites

Do you have a real purpose behind this question or are you just chasing rainbows because you are a 'newbie'?

 

 

 

(What are the 'user codes'?)

 

If you can't manage to read manuals because they are 'complicated', then perhaps you should find a new hobby.  As I've said before, the entire computer-related business is NOTHING BUT technicalities.  Hardware, software - they are both complex.  Sounds like being 'complex' is not your thing.  Perhaps that's why we get the big bucks - because we can read and understand normal English despite the topic.

 

There is such a thing as practice too.  Do you think the first manual we've all had to read was all about Dick & Jane?

 

Good luck in your future endeavors.

 
User code is codes given by the users in their comments in the php manual site.
Tonnes of programmers have told me not to learn from the php manual as it's not a tutorial. Told me to use it as reference but learn from codeacademy.com, phpdelusions.com, etc.. Only a tiny minority like you suggest I learn from the manual.
I will use it as a reference.
Well, atleast the manual you read was not about Adam & Steve! Ha! Ha!
You & Sedopati, can't & won't make me quit. Have come this far. Maybe, best you quit php and this forum ? If you don't like to hear that then don't say things others won't like to hear.
FYI, I will carry-on on this forum, with my questions, like I have been so far (even if that annoys you 2).
(Infact, I might aswell annoy the heck out of you 2 enough for you guys to sigh and come to my call everytime just to shut me up. And, learn from you guys that way! Lol!)
Link to comment
Share on other sites

UPDATE,

 

In the Settings-, in FILTER KEYS- section, I have turned off "Repeat Keys" and I now only get a single "-" (instead of continuous) getting auto typed when I type "s" or "w" or "x".

Anyway, researching:

https://www.google.com/search?q=file+ex-tens-ions+-of+dow-nloadable+files-&oq=file+ex-tens-ions+-of+dow-nloadable+files-&aqs=chrome..69i57.8842j0j7&sourceid=chrome&ie=UTF-8

 

@Ginerjm,

 
The real purpose behind these questions is that I am adding a filter onto my copy of Php-Proxy. And I have a long list of banned words. The filter has to work on each and every page fetched by the web proxy. The filter will use the Explode Function to list all the keywords found on the page and then it will check each line of the keywords one by one against the banned words array. Now, don't want to be Exploding the words by using the space only but other things too like mentioned in my op. So, since you're an experienced php developer then try to atleast figureout what this new bud is trying to do and don't try to haul the poor learner over the rain bow to the land of Oz.
FYI, I will still call myself a newbie until I have learnt the basics. And FYI, I will continue with my adv projects, even if it is way beyond my head. That is my spirit!
Soooome where over the rainbow, blue birds fly. Birds fly over the rainbow, then why o why can't you ginerjm ? Lol!
You know you've put me in the mood to hear that song now. Don't talk about rainbows anymore! Deal ? Lol!
Now you're gonna obsess me wih rainbows. I will see if I can comeup with a new php project something to do with rainbows. Maybe a game of some sort that will be fun foir you guys and a good learning curve for me. Cross your fingers requinix, a flood of highly intelligent questions gonna be flooding your forum. Don't blame me. Blame Jinergm. Lol!
Ok, enough of the chitchat. Just did a little to wake me up as I nearly nodded of my chair a few times within the last 10 mins!
 
EDIT: Gonna try building a php game soon. Been thinking about it lately and wondering what to build. Now thinking: Let's call it: GinerJM.
Howabout 2- twin characters: JinerGM & GinerJM who fight the Ginger Man ? Ok, let the game idea develope in my head and then we'll discuss it in another new thread.
Thanks Ginerjm for unpurposely giving me directions to my new future. I hope the new game makes me a few $millions! Lol!
No, I'm not picking on you Ginerjm. If the game does well, you'll become famous! Lol!
You know, I keep spelling Jinergm sometimes. Is it ok if I call you Ginjerman instead ? That way, I will remember to write the "G" before the "J".
If you forget phps-ane then you can call me sanephp. I don't mind, if that is easier for you to remember.
Link to comment
Share on other sites

Archived

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

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