tobbbe
1/5/2016 - 2:04 PM

parse a string for placeholder text. Measurements, inches, mm, cm Umbraco

parse a string for placeholder text. Measurements, inches, mm, cm Umbraco

public static HtmlString ParseMeasurements(this UmbracoHelper umbraco, HtmlString text, bool useInches)
    {
        var textstring = text.ToString();

        while (true)
        {
            var start = textstring.IndexOf("##");
            if (start == -1)
            {
                break;
            }

            var end = textstring.IndexOf("##", start + 2);
            if (end == -1)
            {
                break;
            }
            
            end = end + 2; // include ##

            var lengthPlaceholder = textstring.Substring(start, end - start);
            var lengthDelimiter = lengthPlaceholder.IndexOf("|");

            if (lengthDelimiter == -1)
	        {
		        break;
	        }

            var length = "";

            if (useInches)
            {
                length = lengthPlaceholder.Substring(lengthDelimiter + 1, lengthPlaceholder.Length - lengthDelimiter - 3); // -3 = ## + |
            }
            else
            {
                length = lengthPlaceholder.Substring(2, lengthDelimiter - 2);
            }

            textstring = textstring.Replace(lengthPlaceholder, length);            
        }
        
        return new HtmlString(textstring);
    }