KeenLearner
KeenLearner

Reputation: 53

combine two functions in php echo

i need to combine these two functions

    <meta name="twitter:image" value="<?php echo(str_replace("367.jpg", "150.jpg", $imageSrc)) ?>" / 
    and
    <meta name="twitter:image"value="<?= substr($imageSrc, 0, strpos($imageSrc, '.jpg')+4) ?>" />

i have tried this

     <meta name="twitter:image" value="<?php echo(str_replace("367.jpg", "150.jpg", $imageSrc)),substr($imageSrc, 0, strpos($imageSrc, '.jpg')+4)?>" / 

while the code has no issues but it renders this

     <meta name="twitter:image" value="https://rlv.zcache.com/seal_of_success_blue_graduation_announcement-r6c3587ec36fd4246afd2add46333186a_6gdu5_150.jpg?rlvnet=1&amp;bg=0xFFFFFFhttps://rlv.zcache.com/seal_of_success_blue_graduation_announcement-r6c3587ec36fd4246afd2add46333186a_6gdu5_367.jpg" / 

whereas i want it to return just one url this

    https://rlv.zcache.com/seal_of_success_blue_graduation_announcement-r6c3587ec36fd4246afd2add46333186a_6gdu5_150.jpg

that is replace 367.jpg to 150.jpg and remove everything after.jpg ?rlvnet=1&bg=0xFFFFFF

Upvotes: 1

Views: 1746

Answers (3)

Mathieu Bour
Mathieu Bour

Reputation: 696

Well, try

For example, I'll take $imageSrc = "http://example.com/img_367.jpg?something"

To be clear, you want first of all remove everything after the ?, an then replace 367 by 150. So try this :

<?= str_replace("367", "150", strstr($imageSrc, "?", true)) ?>

Upvotes: 4

Sarath E
Sarath E

Reputation: 396

Please try this

<?php echo str_replace('_367.jpg','_150.jpg',current(explode("?",$imageSrc))); ?>

Upvotes: 0

sidyll
sidyll

Reputation: 59287

Call one function inside another and it will work:

<meta name="twitter:image" value="<?=
    str_replace(
        "367.jpg",
        "150.jpg",
        substr($imageSrc, 0, strpos($imageSrc, '.jpg')+4)
    )
?>" />

Or do it step-by-step saving to the variable:

# remove tail
$imageSrc = substr($imageSrc, 0, strpos($imageSrc, '.jpg')+4);
# replace size
$imageSrc = str_replace("367.jpg", "150.jpg", $imageSrc)

<meta name="twitter:image" value="<?= $imageSrc ?>" />

Upvotes: 2

Related Questions