473,569 Members | 2,844 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why do my inputs contain an underscore after an ARRAY?

30 New Member
I have the function below to create a tiny url but what happens is that when i run it through I get the out comes with a undescore(_) all except the last output.

Example:
I input

http://sample1.com
http://sample2.com
http://sample3.com
http://sample4.com

I get

http://link.mysite.org/?1234
http://link.mysite.org/?4234
http://link.mysite.org/?3566
http://link.mysite.org/?6756

but when i check them I get linked to

http://link.mysite.org/?1234 => http://sample1.com_
http://link.mysite.org/?4234 => http://sample2.com_
http://link.mysite.org/?3566 => http://sample3.com_
http://link.mysite.org/?6756 => http://sample4.com

Notice that the last one doesn't have the underscore. So im thinking its the array causing problem.

[PHP]<?php
function tinyurl($url){
// Shortens a url
$html = file_get_conten ts("http://link.mysite.org/create.php?url= ".$url);
preg_match('/http:\/\/link\.mysite\.o rg\/(.*)<\/b>/', $html, $matches);
return "http://link.mysite.org/".$matches[1];
}

$url = $_POST['multi_urls'];
$lines = explode("\n", $url);
foreach ($lines as $value){
echo tinyurl($value) .'<br>';
}

?> [/PHP]

Please help.
Nov 8 '07 #1
5 1840
nothing1
30 New Member
I never found out why it does what it does but i found a work-around. Check below.

[PHP]<?php

function tinyurl($url){
// Shortens a url
$html = file_get_conten ts("http://link.mysite.org/create.php?url= ".$url);
preg_match('/http:\/\/link\.mysite\.o rg\/(.*)<\/b>/', $html, $matches);
return "http://link.mysite.org/".$matches[1];
}

$url = $_POST['multi_urls'];
$lines = explode("\n", $url);
$number = count($lines)-2;
for($i=0; $i<=$number; $i++){
$pro = substr($lines[$i], 0, -1);
echo tinyurl($pro).' <br>';
}
$last = count($lines)-1;
echo tinyurl($lines[$last]).'<br>';
?>[/PHP]
Nov 8 '07 #2
Atli
5,058 Recognized Expert Expert
Hi.

Considering that a URL will rarely have a underscore at either end, you may want to try the trim() function on the $value of your foreach loop.
Expand|Select|Wrap|Line Numbers
  1. echo tinyurl(trim($value, "_")).'<br>';
  2.  
Nov 8 '07 #3
nothing1
30 New Member
I was going to try trim but looking at the manual it made it seem like it will remove all the '_' in a URL which makes : http://somesite.com/which_one.php useless. I know it states only from beginning and end of string but then this example doesn't really make sense.

[PHP]
$hello = "Hello World";

$trimmed = trim($hello, "Hdle");
var_dump($trimm ed);

//output: string(5) "o Wor"
[/PHP]

Thanx for the reply BTW
Nov 8 '07 #4
Atli
5,058 Recognized Expert Expert
The example makes perfect sense. Take a closer look and you will see it to.

It strips all the characters listed of the beginning/end one by one, until the character at the beginning/end is not one of the characters listed.

Consider this example, and then take another look at the example you posted:
Expand|Select|Wrap|Line Numbers
  1. $text = "1a2text1a2";
  2. echo trim($text, "12a");
  3. # outputs: text
  4.  
As to your URL problem, putting an URL through the trim function with a "_" as the second parameter will not remove underscores unless they are at the beginning or the end of the URL. (I even tested it to make sure ;)
Nov 9 '07 #5
nothing1
30 New Member
Well now I know how to use TRIM properly and you explained it pretty well. Everything is running smoothly. Thanx Again.
Nov 9 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
4887
by: Dave Smithz | last post by:
Hi there. Because of the lack of a Union query in MySQL 3 I have decided to take the approach where I populate two arrays with values from similar tables in DB. In this case they are `courses` and `lessons` Lets say Courses has fields CourseID(PK), Date, Name, RunBy, Status The lessons table is related to the Courses table in that a...
8
2493
by: kings_oz | last post by:
I have an array or items each item in the array can contain another array of other items and that array of other items can contain more array of item. I want recursively iterate through the array and print out all the items. Can anyone show me some light. Thanks in advance.
5
1905
by: Robert | last post by:
Hi, This might be a strange question but i would like to know how to return an array from a function. Do you have to use pointers for this? Thanks in advance, Robert
1
1398
by: John Dann | last post by:
Is it possible in VB.net to have arrays of structures (ie UDTs) where the structure definition includes one or more arrays? Something like: Structure MyUDT ItemOne () as byte 'for example sub new(byval dimension as integer) redim itemone(dimension) end sub End structure
10
3006
by: wenmang | last post by:
hi, I have following: struct array1 { int id; char *name; }; struct array2 {
3
2498
by: Geoff Berrow | last post by:
Does anyone know what regular expression I would use to split text into an array, assuming the text can be separated by any non alphanumeric character? e.g $string ="cat,dog fish, mouse -elephant/tiger"; would give
1
1191
by: gator6688 | last post by:
I wrote this program and so far so good but now I have to be able to count how many 1's, 2's, 3's, and 4's were entered. I am at a complete loss of where to begin. Could someone help me get started? #include "stdafx.h" using namespace std; int _tmain(int argc, _TCHAR* argv) { const int People = 5; int i, PeopleTypes, count=0;
2
1690
by: Rick Giuly | last post by:
Hello All, Case 1 This generates an error, which makes sense because the argument should be a list of numbers: numpy.array(10,10) Case 2 This does not generate an error and the result is an array with a single element:
2
1233
by: rezanew | last post by:
Hello, I am developing an application for windows that is going to upload images to a free image hostings... I am using WebClient and UploadData for posting my form to the website... now lets take a look to one of these free image hostings , for example img98.com.. as you can see in the main form for uploading there are 4 inputs with the...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7922
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7668
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5218
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1209
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.