FRIHOST • FORUMS • SEARCH • FAQ • TOS • BLOGS • COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Get specific parts of a string





Dougie1
Is there a way to get a specific part of a string into a variable. For example if I wanted to get the parts of a string after image=" and before " in the following:

loasjlkciojsifjjiosaf image="www.image.net"dfjskldfjsdfsidjfosdjfiojsdiofj

What code would I use to get the following part only into a variable:

www.image.net

Please note that the parts before and after image are not a specific string legnth so it would not work to use substr.

Thanks.
powers1983
Code:

$string = 'loasjlf image="www.image.net"dfjskldfjsiofj';
$start_of_search_string =  strpos($string, 'image=" ');
$search_char = $start_of_search_string + 7;

$completed = false;

while (!$completed) {
   $read_char = substr($string, $search_char, 1);
   if ($read_char != ' " ') {
        $image_name = $image_name.$read_char;
        $search_char++;
    } else {
        $completed = true;
    }
}

print "Your image name is $image_name";


Hopefully that should work. Note that I put a few spaces in between the different types of quotes that you'll need to take out - it was hard to see what quotes were used.

You might need to watch out because if the string is escaped (ie a \ before the ") then you'll need to take account of that in your searching.

David.
Ranfaroth
Dougie1 wrote:
IFor example if I wanted to get the parts of a string after image=" and before " in the following:

loasjlkciojsifjjiosaf image="www.image.net"dfjskldfjsdfsidjfosdjfiojsdiofj

What code would I use to get the following part only into a variable:

www.image.net
Code:
preg_match("/^[^=]*=\"([^\"]+)\"/",$input,$output);
Result in $output[1]
fromegame
Code:
<?php
$string = loasjlkciojsifjjiosaf image="www.image.net"dfjskldfjsdfsidjfosdjfiojsdiofj;

$exploder = explode("image=\"", $string);
$exploder2 = explode("\"", $exploder[1]);

$string2 = $exploder2[0];

echo $string2;

?>
MrBlueSky
Ranfaroth wrote:
Dougie1 wrote:
IFor example if I wanted to get the parts of a string after image=" and before " in the following:

loasjlkciojsifjjiosaf image="www.image.net"dfjskldfjsdfsidjfosdjfiojsdiofj

What code would I use to get the following part only into a variable:

www.image.net
Code:
preg_match("/^[^=]*=\"([^\"]+)\"/",$input,$output);
Result in $output[1]


This is a correct solution. But you might want to take care that only image= is matched and not other assignments. Also you don't want to check for the first occurance of '=' after the beginning of the line:

Code:

preg_match("/image=\"([^\"]+)\"/i",$input,$output);


For example, this will correctly process the following line:

Code:

==name="john", image="test.jpg", value="3" ==


putting test.jpg in $output[1].

If the value of image may be an empty string (image="") use this:

Code:

preg_match("/image=\"([^\"]*)\"/i",$input,$output);



You might also consider making sure that extra spaces around '=' are allowed.
Ranfaroth
You're right, but we could go further Wink
Your regex will fail with the input string .... largeimage="..." name="..." image="...".

Enhanced regex : "/(?:^|\s)image\s*=\s*\"([^\"]*)\"/i"
Related topics
How Do You Center Your Entire Web Page?
•What the mean of Globalization
i want thinner legs
Idea for an Experimental Game
Building Your Own PC?
:: whats your favorite lyrics ::
Most Christians do not read the Bible, this is a fact!
Keeping a good tone
music editing software
Virtual vs Reality
Scientific analysis of religion...possible?
Do you have any faith that there is a "soul"
House votes to repeal Obama's Health Care Law
Can electricity boost mathematical talent?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.