PHP - Splitting A String
Okay I need a little bit of help on this as strings constantly confuse me and reading tutorials actually got me a bit more loss then before. I have developed this mod that has become pretty popular and I'm debugging it. What I have are two variables.
$username (The user's username, no html markup in it) $musername (The user's username, with html markup in it) So for example: $musername = <b><u>$username</u></b> The php code above would be the equivolant to what $musername is equal to. What I need to do is create an array or two variables, whatever is easier and grab all that html markup. The first variable would be all the html or anything really before the $username variable, and the other would take all of that html after the $username variable. I need to store those html markups in either two seperate variables or an array so i can call upon it later in the script. I need this to fix a bug issue. Similar TutorialsThe sample string is KmSO4 Every upper case alphabet should be identified and splitted until another upper case alphabet comes. the string should be splitted so that output array looks like this Array ( => Km [1] => S [2]=>O4 ) can any one help regarding this issue? I have a string like this: Code: [Select] Something,Somthing Else,"keyword1,keyword2,keyword3",Yet another I need to split that string by the ,. However, I need the data that is n the quotes to remain as one even though it has commas because it is one item. I assume I need to use preg_split and have tried some code but none of it seems to work properly. If anyone has a better method or knows how I can split this up using preg_split the help is appreciated. I have a database full of strings which are basically URL's with ports, so by default my strings look like: http://example.com:1234/ http://example2.com:3030/ http://example.com:9876/ http://example.com:5432/ What I am looking for is a way to separate the host from the post so the output would be something like: Host: example.com Port: 1234 Note: I once need to check one string at a time, so what I am after is something like: $string = "http://example.com:1234/"; And the result would be something like: $host (would be example.com without http://) $port (would be the port without ending /) Any help would be great Thanks for your time reasing this. Hello, I need to find the target html address within some raw html. I know it lies between Needle 1 and Needle 2 below. Needle 1: </span></a> <a href=" Needle 2: " rel="nofollow"><span class=pn><span class=np>Next »</span></span></a></div> Nornally I would just use the explode() function but my headache is that there are many instances of Needle 1 in the raw html code and I want my output array always have the same number of elements. However I do know that my target html address is directly after the last instance of Needle 1. How should I go about splitting the html code into an array that always has 3 elements? I'd like the first element to include code up to and including the last instance of Needle 2, the 2nd element to have the target html address, and the 3rd element to have Needle 2 and any following code. Thanks for any pointers (my main struggle is identifying the last instance of needle 1), Thanks, Stu Heya guys! What's up? OK here's my problem: I got a variable, $var1, which is to have values like "groan,100,200,350,50" I need to know how to split this $var1 into $split1, $split2, $split3, $split4, $split5, where $split1 = "groan"; $split2 = "100"; $split3 = "200"; $split4 = "350"; $split5 = "50"; Thanks in advance people! P.S. In my case I am going to use some of the values for calculations. So I don't think that its easy with arrays, which are difficult for me to understand . P.S.S. When I am retrieving data from the database, many variables like $var1 that need to be split are being extracted. I hope that you understand. Thanks and Regards, Thauwa http://mobsters-fb-apache-dynamic-lb.playdom.com/mob_fb/get_hit_list?user_id=100000952273457&target_id=100000556624340&level=300&auth_key=cb07c7575b38df48d82dd53619385faa884db5a2 When going to this URL, it gives entries between <entry> and </entry>, what I am trying to do, is collect the <user_id> ONLY if the <amount> is over a certain value. Any ideas to how I could do this (I am a complete PHP noob). All help is appreciated I have a text field that contains the following (example text used): 1 2 3 4 5 6 As you can see the first few are split by a single space, with the last few numbers split by a double space. My aim is to put each of these numbers into a table in a MYSQL database. But I'm having trouble breaking them up. Hi Guys, does anyone know how split a text into groups depending on how many words are in the text e.g $text = "If you are new to PHP and want to get some idea of how it works, try the introductory tutorial. "; I want a maximium of 5 words in each group, so the above text will be split as such $text1 = 'If you are new to'; $text2 = 'PHP and want to get'; $text3 = 'some idea of how it '; $text4 = 'works, try the introductory tutorial.'; But if I want a maximium of 10 words in each group, so the above text will then be split like this $text1 = 'If you are new to PHP and want to get'; $text2 = 'some idea of how it works, try the introductory tutorial.'; $text3 = ''; $text4 = ''; Is this possible? Please any help will be much appreciated thanks Hi guys Say for example I have a text field with value 20/10/2011 how do I define it so that $x=20, $y=10 and $z=2011 so that I have three variables Thanks I could use some suggestions on how to break up my HTML and PHP. Currently I have a file called "payment_form.php" It looks like this... Code: [Select] <body> <?php // HANDLE FORM. if (isset($_POST['submitted'])){ // Handle Form. // Determine if any errors. if (empty($errors)){ // Process Payment. // PHP code to send payment to Payment Gateway... // Do not return to Payment Form!!! exit(); } } ?> <!-- HTML PAYMENT FORM --> <form id="payment" action="" method="post"> <fieldset> <legend>Billing Details</legend> <ol> <!-- First Name --> <li> <label for="firstName">First Name:</label> <input id="firstName" name="firstName" class="text" type="text" maxlength="20" value="<?php echo $firstName; ?>" /> <?php if (!empty($errors['firstName'])){ echo '<span class="error">' . $errors['firstName'] . '</span>'; } ?> </li> I would like to keep the Form Validation PHP in the same file as the HTML Form so the user isn't ping-ponged between two pages if they make data entry errors. However, I am thinking it would be better to break out the Payment Processing PHP and place that in a different file. How should I do that? Maybe I could add a Redirect after this code... Code: [Select] if (empty($errors)){ // Process Payment. // PHP code to send payment to Payment Gateway... Suggestions welcome!! Thanks, Debbie I'll be creating a script that will get the keywords from uploaded pictures and what i cant figure out is how to actually get only the keywords from the iptc data this is the code i have for getting the iptc data right now: Code: [Select] <?php $size = getimagesize('images/Sections/pinkpanthers/20102011/Session1/1-15-11/1.jpg', $info); if(isset($info['APP13'])) { $iptc = iptcparse($info['APP13']); var_dump($iptc); ?> } and this outputs Code: [Select] array(4) { ["1#090"]=> array(1) { [0]=> string(3) "%G" } ["2#000"]=> array(1) { [0]=> string(2) "" } ["2#025"]=> array(1) { [0]=> string(5) "Tyler" } ["2#080"]=> array(1) { [0]=> string(15) "Christian" } } all i want from the above string is the Tyler I have a problem displaying the results of a table, It shows them in one long list and I'd like them to show as 2 or 3. I had a look around and the most favoured way to this I found was to split the results either by odd/even or using percentages but Im having trouble implementing it into my script. How would I go about adding the odd/even way to this: <?php $query = mysql_query('SELECT * FROM users ORDER BY Username'); while ($row = mysql_fetch_array($query)) { if ($row['UserID']) ?> <b>Username:</b> <span class=class1><a href="aeditprofile.php?username=<? echo $row['Username'] ?>"><? echo $row['Username'] ?></a></span><br> <b>Group:</b> <? echo $row['Level'] ?></color><br /><hr> <?php } ?> Essentially I'm after getting something like this: Name Name Name Group Group Group and so on down the page.... Hi All How do I go about splitting a multidimensional array into seperate smaller ones? Currently I have $grade[region][userid]=value. I'd like to split as $region1[userid]=value $region2[userid]=value $region3[userid]=value $region4[userid]=value $region5[userid]=value $region6[userid]=value Is there an inbuilt function for this? hey guys just a quick onei having a blank moment i have an array at themoment pulling from a database. i basically want to split this array based on a min and max value. divide it equally and then give it a value on each smaller array and where each array starts... to make it clearer - say i have an array with 600 values in i want it split up into 8 smaller arrays link so Array ( => Array ( [start] => 0 [split] => 75 ) [1] => Array ( [start] => 75 [split] => 75 ) [2] => Array ( [start] => 150 [split] => 75 ) carrying on till the end ) how would i do this. Hi, I am trying to split a string into an array and then seeing what sort of data it is. eg; I get the following string Quote api=somerandomkeyhere&user=someuser&password=somehashvaluehere&type=somethingelse I need to be able to put this into an array so in this case Code: [Select] myarray[type]=somethingelse myarray[api]=somerandomkeyhere myarray[user]=someuser myarray[password]=somehashvaluehere but it could include many more/less different value's separated by the & symbol. Thanks, -mme Hey guys,
I have an array of names sorted alphabetically. I'm trying to loop through them and create new arrays keyed by the letters of the alphabet containing the names which start with that letter.
so in the end i'm trying to create something like this:
$newarray = array( 'a' => array('anna','alan','andrew'),
'b' => array('bert','ben','bob'), 'c' => array('cait','carry'), etc,etc ); thanks Hi, I have a form with a dropdown Code: [Select] <select name="county" id="county"> <option selected="selected">Choose A County</option> <option value="1">Dublin</option> <option value="2">Westmeath</option> <option value="3">Galway</option> <option value="4">Roscommon</option> </select> What I am trying to do is seperate for example the value '1' and the option 'Dublin'. The results are passed to hidden field on a second form page. This is what I have, but it doesnt seem to be working... Code: [Select] <input type="hidden" name="county" value="<?php echo ($_POST['county']); ?>" /> <input type="hidden" name="county1" value="<?php echo ($_POST['county']); ?>" /> so I was wondering if anyone might be able to give me a pointer in the right direction. I have tried using an array on the processing page to split it but its not working either.. like this: Code: [Select] $county = (float) $_POST['county']; $county1 = in_array($county, array(1, 2, 3, 4)) ? $county : 5; $county1 = $_POST['1']='Dublin'; $county1 = $_POST['2']='Westmeath'; $county1 = $_POST['3']='Galway'; $county1 = $_POST['4']='Roscommon'; $county1 = $_POST['5']='Cork'; I have been trying to get this to work for a few days and I have searched everywhere for the answer, but I can't find it anywhere. I would be so grateful if someone could help me with this and it the last part I need to do before I move on to the next part of my project. Thanking you Eoin Hello, I have a variable that holds a piece of text that looks something like this: "world,blue,big". Is there anyway to split the text into the separate words as an array, and remove the commas? Code: [Select] $text_to_split = "world,blue,big"; $splitArray = array(); //Do some kind of php function that splits text\\ echo splitArray[0]; // world echo splitArray[1]; //blue echo splitArray[2]; //big Would appreciate it, thanks hi, how is everybody doing? i hope well, i have a problem with some php code. i am trying to split some text in two parts once they have been split, they are display, but sometimes the php code break the source code of the page such as images and instead of the image i get the source code like this Quote add-plugins-paintnet-1.2-800x800 3857cff9733e6a.jpg" style="border-width: 1px; border-color: #000000; border-style: solid; margin: 4px auto 5px; display: block;" /> i have tried for about 2 days now to fix it and i haven't been able to, so any help or guidence would be greatly appreciated. this is the code that i am currently using, i have tried to change the variable in the $findTag but nothing seems to work Code: [Select] <? php $article =$this->article_>text; $countchars=strlen($article); $divide=$countchars/2; $firstpart=substr($article,0,$divide); $secondpart=substr($article,$divide); //i keep changing the <p /> variable to <img /> and $image and many others but nothing seems to work $findTag = strpos($secondpart, "<p /> "); $var = $divide + $findTag $firstpart = substr($article, 0, $var); $secondpart = substr($article, $var); ?> thanks Hi I have a function that takes a postcode and splits it so i can enter into DB into two formats, full postcode and prefix. The function is: function check_form_postcode($postcode) { $postcode = strtoupper(str_replace(chr(32),'',$postcode)); $suffix = substr($postcode,-3,3); $prefix = substr($postcode,0,(strlen($postcode)-3)); if (preg_match('/(^[A-Z]{1,2}[0-9]{1,2}|^[A-Z]{1,2}[0-9]{1}[A-Z]{1})$/',$prefix) && preg_match('/^[0-9]{1}[ABD-HJLNP-UW-Z]{2}$/',$suffix)) { $postcode_syntax_check = 1; } else { $postcode_syntax_check = 0; } if($postcode_syntax_check){ $return = $prefix."::".$suffix; } return($return); } I POST the form data and in the top of the same page I get all of the form data and here is is where things go wrong: if ($b == 'go') { //Gets form data $main_event_name = my_import('main_event_name', 'P', 'TXT'); $main_event_type = my_import('main_event_type', 'P', 'TXT'); $main_event_date = my_import('main_event_date', 'P', 'TXT'); $main_event_city = my_import('main_event_city', 'P', 'TXT'); $postcode = my_import('postcode', 'P', 'TXT'); $main_event_added = my_import('main_event_added', 'P', 'INT'); $main_event_active = my_import('main_event_active', 'P', 'INT'); $postcode_array = check_form_postcode($postcode); $postcode = explode($postcode_array, "::"); $prefix = $postcode[0]; $fullpostcode = $postcode[0]." ".$postcode[1]; $valkey = md5(microtime()); $info = array( "main_event_name"=> $main_event_name, "main_event_type" => $main_event_type, "main_event_date" => $main_event_date, "main_event_city" => $main_event_city, "main_event_active" => $main_event_active, "main_event_added" => $sys['now'], "main_event_pcode" => $fullpostcode, "main_event_pcode_prefix" => $prefix, "eo_id" => $eo['id'] ); $table = $db['main_event']; $userid = my_insert($info, $table); When I enter into the DB all I get is the "::" no data. I know that the POST works as I can enter the data easily without the function going wrong. Thanks for any help, i am new to arrays go easy if I have made an obvious mistake! Cheers... |