skip to Main Content

i am trying to insert an image url into long text in laravel blade file but how to split the text int two equal parts and insert the image in the center of the text

i use str_split but it spit the text to many parts, can anyone help please ??

2

Answers


  1. Find the answer with the example of code:

     // Long text
    $longText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium.";
    
    // Image URL
    $imageUrl = "https://example.com/image.jpg";
    
    // Split the text into an array of characters
    $textArray = str_split($longText);
    
    // Calculate the halfway point
    $halfway = ceil(count($textArray) / 2);
    
    // Insert the image URL at the center
    array_splice($textArray, $halfway, 0, $imageUrl);
    
    // Join the array back into a single string
    $textWithImage = implode('', $textArray);
    
    // Output the result
    echo $textWithImage;
    

    In this example, the image URL will be inserted at the center of the long text. You can adjust the position or modify the code as per your specific requirements.

    Make sure to replace $longText with your actual long text variable and $imageUrl with the URL of your desired image.

    Login or Signup to reply.
  2. I don’t get what you want exactly, but this may can help you:

    @php
            $text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed at bibendum dolor. Phasellus id eros non ante iaculis congue. Sed finibus mi sed tellus pretium, nec ultrices nisl ultrices. Aliquam erat volutpat. Sed eget condimentum mi, vitae pellentesque libero. Sed tincidunt, justo vitae iaculis rhoncus, purus diam lobortis augue, et tempor turpis dolor sed tortor. Vivamus iaculis vestibulum fringilla. Curabitur id convallis dui. Nam convallis nisi et velit efficitur, a eleifend sem auctor. In hac habitasse platea dictumst.";
        
            $imageURL = "https://example.com/image.jpg";
        
            $midpoint = strlen($text) / 2;
            $firstHalf = substr($text, 0, $midpoint);
            $secondHalf = substr($text, $midpoint);
        @endphp
        
        {!! $firstHalf !!}
        <img src="{{ $imageURL }}" alt="Image">
        {!! $secondHalf !!}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search